Global variable ?

Is it possible to declare a global variable ?
i.e a variable that is available anywhere with the application, but can be changed.

If it is possible how do you do it ?

Regards
Eddie

Create a Module.
Within the Module create a Property.
Set its scope to global.

1 Like

either as Eliott suggested OR you declare the variable under app and then use “app.myvariable” syntax, adding app. before the name of yr variable. (variable are called proprieties in Xojo).
But I’ve bee told that using the module is more “professional”

[quote=125405:@Horacio Vilches]either as Eliott suggested OR you declare the variable under app and then use “app.myvariable” syntax, adding app. before the name of yr variable. (variable are called proprieties in Xojo).
But I’ve bee told that using the module is more “professional”[/quote]
This is actually an instance property - not a global variable.
It’s just that there is one & only one instance of app.
When you put it in a module & make it global its truly a global variable - but I’d discourage using a lot of them.

You get a global! And you get a global! Everyone gets a global variable!

[quote=125407:@Norman Palardy]This is actually an instance property - not a global variable.
It’s just that there is one & only one instance of app.
When you put it in a module & make it global its truly a global variable - but I’d discourage using a lot of them.[/quote]

why is global variable bad in Xojo?

It’s not that global variables are ‘bad’, it’s just that if you’re using a lot of global variables you may be causing some issues down the road.

The best (poor) example I saw once was a global rs as RecordSet. The developer reused the same Recordset for hundreds of different queries. Since it was global and it was used everywhere it led to exceptions and other things that were hard to figure out. As soon as he opened up more than one window (where rs was used on each one) things went screwy.

If you need a global variable you need one. However, it’s been my experience you don’t need many. Even in my biggest desktop apps I sometimes only have one - the database object.

Same reason it’s bad in any other language
http://en.wikipedia.org/wiki/Global_variable

Once upon a time I took over a project from a collegue. He had used globals everywhere. The funny part was that he used always 2 similar values like (2, 3, 4) and then (x, y z) where x was the parameter value for 2. It took me weeks to get the code straightened out and since then I ALWAYS prefix my globals with “global”.

Wow, Beatrix. I’ve seen some bad ones, but I think that might take the ‘prize’. Makes you wonder what people were thinking. What surprises me even more is how these projects ever worked in the first place. (But then I’ve made a lot of money over the years fixing others mistakes so who am I to complain?).

We tend to prefix our global variables with ‘g’ but we break that rule often. More and more we’re putting them into an object of some sort to eliminate multi-user issues.

[quote=125459:@Bob Keeney]It’s not that global variables are ‘bad’, it’s just that if you’re using a lot of global variables you may be causing some issues down the road.

The best (poor) example I saw once was a global rs as RecordSet. The developer reused the same Recordset for hundreds of different queries. Since it was global and it was used everywhere it led to exceptions and other things that were hard to figure out. As soon as he opened up more than one window (where rs was used on each one) things went screwy.

If you need a global variable you need one. However, it’s been my experience you don’t need many. Even in my biggest desktop apps I sometimes only have one - the database object.[/quote]

instead of using global properties… i start using dictionary instead. is that a good idea??

when i did use global variable, i always start with g and when i used global constant start with c

[quote=125466:@Norman Palardy]Same reason it’s bad in any other language
http://en.wikipedia.org/wiki/Global_variable[/quote]

i know on Ms Access, when you encounter error, the global variable is all gone… disappeared.

How does that help? It seems like it would have the same problems, and be more obscure than using global properties.

You do not need to make your dictionary global.

I agree with Bob : one or two global variables is often enough. And prefixing them is preferable.

Global variables is often the result of bad habits caught using old fashion Basic like Applesoft Basic, Basica and GWBasic which did not have the concept of scope (or anything OOP for that matter). I am grateful to RealBasic to have taught me fairly effortlessly all the concepts needed in modern programming, without being too extreme.

I agree with Tim and Michel.

Look, you’re probably worrying too much about it. Things like app preferences should be global to the app. That’s what preferences are for.

However, if you have a global variable x and you’re using x in different places for different reasons that’s bad. X should be local to the part of the app that uses it (of course make it a better name). Reusing a bunch of variables will result in bugs and sometimes very subtle. In testing of a single area of the app it’ll be fine but when you have multiple windows open (that all use the same global variable) you’l start to see problems.

Preferences should be a module you can call that doesn’t require a global variable at all :stuck_out_tongue:

most of my global variable is in the table. So instead of opening the table many time to read those value, i put them in global variable and then use it when i need to. recently i change this to assigning to a dictionary when the application is opened, and then use the value from it.

I never use a global recordset and use the same variable for each screen. The only time i used global variable if when i pass over a string of IDs from one windows to another.

In that case, a dictionary is a good choice (for this specific use) in that you can use arbitrary names for your preference items. What I do is hide the dictionary and expose a set of getter/setter methods. Eg.,

prefs(“name”) = value
value = prefs(“name”)

I overload the methods to accept different data types so I don’t have to think about converting when I use them. And the fact that everything is name based means if I want to save a new value, I just make up a new name and go with it. I don’t have to add a property or modify a class, or even modify the database table. It just works and I don’t have to think about the mechanics. It’s magic when you can save a value and then retrieve it the next time the program runs, with zero programming effort.

Tim, so how do i hide the dictionary and expose a set of getter/setter methods?