TOPIC: Global Variables?
#795
Global Variables? 3 Years, 5 Months ago
After reading the Manual and trying a few things I can't seem to get figure out, is it possible to declare a standalone variable in a library?

Part of me suspects that global variables don't exist and such an item would need to be a field in a table, but I am not sure.

Or if there is an example of such a variable in the stdlib then if anyone can kindly let me know the name of it, I will look it up and figure out how it works.
 
 
Scotty
 
#796
Re:Global Variables? 3 Years, 5 Months ago
Hi, global variables are defined as items in the library you use. For example, in LibraryView, right-click on a lib -> New -> Item, and then write the name of your global var. In the editor on the right you can fill directly the variable value (that has to be a constant value, i.e. it cannot be a dynamic Object or a function call).
 
 
Mauro
 
#797
Re:Global Variables? 3 Years, 5 Months ago
As Mauro says, libraries are the primary method of defining global values like functions and classes. Libraries can also contain objects, numbers, and strings.

However, library globals are "constants" - you cannot assign to them in your code. **

The only global variable facility is the Suneido object. Because this single object is shared with all code you need to be careful with the member names you use. If you need to store multiple values it is a good idea to put them in an object and store this as a single Suneido member. Run: Inspect(Suneido) from the WorkSpace to see what stdlib puts in there.

In a client-server situation, each client has it's own Suneido object. If you need to store variables that are accessible by all clients you can do things like: ("Suneido.my_shared_var = " $ Display(var)).ServerEval()

In general, my feeling is that global variables should be avoided.

Some languages allow class variables which are then global. In Suneido classes are constants. You can put values in them that are globally available, but you cannot modify them at run time.

And, as you thought, you can use the database to store global information, although this would generally be used for "persistent" information that had to be kept long term. The database is not really designed for "variables" in the normal sense.

** It is possible for code to update records in libraries and change global values, but this is not recommended. It will not work properly in a client-server situation because clients cache the value of library globals.

Post edited by: andrew, at: 2007/03/11 17:57
 
 
andrew
 
#799
Re:Global Variables? 3 Years, 5 Months ago
Thank you for the answers. I certainly like to avoid global variables in my code, I am thinking about different ways to store "state" information when switching between different pages in a book.

It sounds like a fields in a table are the way to go. Although the Suneido object sounds interesting to check out.
 
 
Scotty