variables

Ok,

I have read the documentation on variables… they lose their value when the method or routine that calls them ends… but I need variables that DO NOT lose their value from one method to another. I use them to store flags, values used in other calculations, etc…

How do I do this with XOJO???

You can create a property that is part of an instance of a class, and it will be available to all of the methods within the class. Depending on the scope of the property it may also be available to instances outside of the instance that it is part of.

But it would not be available to use within other forms in the program? I am not really sure to do what you are suggesting…

[quote=20834:@Gary Vassalotti]Ok,

I have read the documentation on variables… they lose their value when the method or routine that calls them ends… but I need variables that DO NOT lose their value from one method to another. I use them to store flags, values used in other calculations, etc…

How do I do this with XOJO???[/quote]

Well they lose their value when they go out of scope - and SCOPE is VERY important
If you have a method like (it doesn’t do anything useful really)

Sub Foo() dim bar as integer bar = 1 end sub

and some other part of your program calls it the the local variable bar exists while that method is running.
It gets created, then its value gets set to 1, and when the method ends it gets destroyed because its gone out of scope.

So to have something persist from one run of the method to another it need to have a scope that is “outside” the method so it CAN persist.

And this IS where it gets tricky - figuring out the right scope can be challenging.
You could make EVERYTHING GLOBAL (which is a really bad idea) but then it would be available to everything everywhere all the time.
You can have thing be available just to a single method (see above) or to all methods in a module or window.

I have no idea how experienced a developer you may be so …
The User Guide Fundamentals has a section about this (page 71 or so)
As does the Introduction to Programming Book (page 115) available here http://xojo.com/download/extras.php

Yes, a property can be declared as global within (as an example) Window1.

  • Within the logic of Window1, it is available
  • to other windows, classes, modules it would be available as Window1.someProperty

While this works, it is not a good practice to utilize. Instead, do some research into encapsulation. Create a property of
window1 that is private, then create 2 methods that are global. A getter and a setter…

getProperty() returns String
setProperty(newValue as String)

I hope this helps, additionally I recommend reading the references cited above.

Place the variable in app.

I’d disagree as

  1. generally not the correct place if you want it to be “global” - make a module & make it global in there otherwise your new variable is actually a property of your app class
  2. in general globals are not a desirable thing as they can lead to obscure bugs

Better to learn & use scope properly