Welcome Mark,
Question/Answer 1.) - Where you declare a variable in Xojo, it is relevant only to the logic required of the given event. Also please see the answer to question 2.
Answer 2.) - Variables declared in an Event Handler are strictly local and will go out-of-scope at the end of execution of the given Event.
Answer 3.) - Keep in mind that a variable of type Integer
(that has no explicit assignment yet) has a default value of 0
.
So: vMode = vMode + 1
gives you 0 + 1 = 1
. Your code example does not increment the value of vMode
higher than 1
.
Answer 4.) - Yes, when using the Dim
or Var
keywords for declaring variables in an Event Handler (or Method for that matter), the variable is re-initialized on each call of the Event.
Alternatively, you can prevent “resetting” of a variable value by using the Static keyword. But be careful using Static
variables, their use can get confusing. For example, when using sub-classed controls, because the Static
variable value could be the same in all instances of your sub-classed control (depending on how you use them).
Another choice is to add a Property to the control or object. A properties value is retained across Events or Method calls (of the given control or object you attach the property too). Property values will also be potentially different in each instance of the control or object, which is different behaviour than a Static
variable.
Answer 5.) - Xojo Methods are the same as a Sub
or a Function
. The only difference is that a Function
returns a value, whereas a Sub
does not. The Xojo IDE simplifies this by calling them both Methods and you decide whether to specify a return type (in the Inspector panel).
Answer 6.) - To manage global variables of almost any type, attach them as Properties
to either the default App
Module/Class that comes with a starting project, or add a Module to your project. A Module
acts like a global space to put values, data or even Methods that need to be accessed globally in your project.
I hope some of this helps. Have fun
Edited for grammer.