How to access variables between items?

Another rookie question: I have an application that has several items in it - radio buttons, text boxes, push buttons, etc. So far all my code works within the “action” for individual items. But, how do I access information between them? For example, if in a push button action I want to set a variable, that variable is not found unless I DIM it there. And if I do that, it is not seen in the action code for another button. Is there such thing as “global” variables? If not, how can I see data between the various action code groups?

Tnx, Jim

I think if you read the getting started guide or whatever it’s called you will find useful info about how many fundamental things work in Xojo, and it would be worth your time.

But to answer your question, variables have a “scope” which relates to where they are declared. If they are declared in a method or event they are local to that specific event or method.

To have a variable accessible to multiple events or methods, it needs to be declared in a larger scope.

One common place is a property of the window that your controls are on. This would be available to any code contained in that window. So it could be a method of the window, or a control placed on that window.

There is also the app class. Declaring a property in the app class would make it available anywhere in the app by using a reference such as app.property.

HTH

What you want to do is add a Property to the window. A property is just like a variable declared in a method with the Dim keyword, except that it’s persistent (until the window closes) and callable from anywhere inside the window.

To further Kevin’s post:

The guide I would recommend for you is the “Xojo: Introduction to Programming Textbook”
http://www.xojo.com/learn/

It gives a nice overview of programming basics, while explaining how they’re handled in Xojo.
You basically learn to program and use Xojo at the same time.

After that, there’s a whole manual to read in the folder titled Documentation within your Xojo install.

Thanks guys - I’ve been reading that guide, but either didn’t get that far, missed that part, or had a brain drain! Will go back to the books.!!!

You could have legitimate global variables but we really try to avoid that. A property on a Window, as suggested above, is the quickest way to help you achieve this immediate goal.

Just right-click on the Window in the Navigator (list on the left), select “Add Property” and you can then refer to that property/variable from any controls that are on that window (that’s its “scope”).

The guide that Tim mentions is great for beginners, which we all were once.

Got it - works now. Went back to the intro to programming textbook and didn’t see where they talked about adding variables in the Window property as being globally accessible, but no biggie; now I know!

Thanks again!!!

They’re not. They’re accessible only to the window and its controls.

"Just right-click on the Window in the Navigator (list on the left), select “Add Property” …

What window on the left please … I click on everything and cannot see “add property” :frowning:

Ah … I think it is “Add to [name of window” … and from there select property …
I made a string there called petpeeves2
I have a command button on the same window in which I have
studentinput.text = petpeeves2

but I get nothing … no error, nothing.

sad face.

Did you put that command into an Event Handle of the Push Button?

I just tried with putting the “studentinput.text = petpeeves2” into Event Handler “Action” and works as expected

[quote=405036:@Philip McCarthy]Ah … I think it is “Add to [name of window” … and from there select property …
I made a string there called petpeeves2
I have a command button on the same window in which I have
studentinput.text = petpeeves2

but I get nothing … no error, nothing.

sad face.[/quote]
So you created a property called petpeeves2. Did you assign a value to it? You can either do this in the Inspector on the right, in the Default field, or you can do it in code. If you don’t assign a value, it will simply be an empty String.

You need to look into the term SCOPE… an application have many levels

  • APPLICATION level … this can be achieved by placing your DIM in a MODULE, these will be available GLOBALLY
  • MODULE level … this can be done by placing your DIM in a MODULE and using the PRIVATE designator, these will be available ONLY to other code in this same module
  • FUNCTION / SUB Level - if the DIM is inside a FUNCTION or SUB, then it will be available ONLY to that Function or Sub, the same pertains to using them in a control EVENT

This is just a highlight, and normally each variable loses its value when it “goes of of scope”… (Investigate STATIC for a way to handle this)

There is much more to knowing how SCOPE works, but this should start you in the right direction.

So you created a property called petpeeves2. Did you assign a value to it?

It’s a string (I put it in the inspector)
In the text area I write
petpeeves2 = “Hello” (just to test it)

In the command box, under “action” I have
studentinput.text = petpeeves2

Oh - if I write the value “Hello” in default it works …
But if I have in that text area: petpeeves2 = “hello” then I get nothing
What is the text area for?? I cannot change the string for petpeeves2 there?

Philip,

If you haven’t already I highly encourage you to visit Get Started to help in the learning curve

Yeah … I did VB6 for years and years … and it’s a transition to xojo … it’s a transition

Those resources should help you in the transition :smiley:

You can also change that value from outside of that window, but be careful with that. Depending on your usage, this can become buggy. In that case, you have to add a Module and place your variable there. Set its scope do… and call it using the module name as Prefix (like: My_Module.My_Variable).

Usually, I add a suffix to know what that variable type it is (FI for FolderItem, Cnt or Nbr, etc.).
I also add some prefix 'TF for TextField, TA for TextArea, LB for Listbox…

At last, sometimes I use some letters because I want the variable be displayed alphabetically in the Navigator Pane…

Every developer have its own ways, I even saw code with the default PushButton1… PushButtonz default values… :wink:

[quote=405047:@Philip McCarthy]Oh - if I write the value “Hello” in default it works …
But if I have in that text area: petpeeves2 = “hello” then I get nothing
What is the text area for?? I cannot change the string for petpeeves2 there?[/quote]
I think that text area is just for notes.
You can’t change the value there, you need to put it on the right on Default.