Declaring and Use of Variables

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 :slight_smile:

Edited for grammer.

2 Likes

Hi Mark, and welcome in the Xojo community.

It all depends on where you want to use the variable. You can also add Properties in a Window / Module / Class. Properties are variables that are accessible within the object (Private/Protected) are accessible outside (Public).

In this case you would need a Window property. Initialize it in the Open event, use it in the Action event.

Here you would need a static variable or window property to keep the state of vMode between each push on the button.

Yes, it is initialised each time the event handler is called, unless you declare it as Static.

In Xojo that would be a Method.

If the arrays and variables are only accessed by the controls in that Window, add them as properties in the Window.
If you need to access them from another Window, you could add them as properties in a Module.

2 Likes

Thanks Scott and Jeremie for your detailed answers. I read them very carefully and they are most helpful.
You will have to excuse my noob questions.
My coding experience has been as follows -
1985 Apple basic
1990 Turbo Basic
1993 Sold a Debtors system programmed in TB, including a host of features
(32000 lines of code, 34 modules)
1994 Sold a stock control system done with TB
1996 Began VB4 and html
1999 Began VB6
1999 Started a design and web studio
2001 Began CSS
2016 Came up with unique ideas for apps
2018 Depreciated my design studio and changed main focus to coding
2018 - 2020 Created Development Tools using VB6 including a code analyser which is so useful
2021 Now solely focused on app development - VB6 and now Xojo
2021 June Version 0.1 private release for a new and very exciting app (A)
2021 Aug / Sept Projected Early Access Release for a Book browser with multiple features
2022 Jan Version 0.5 Early Access Release for app (A) [which will change the way people do a certain computer function - it’s secret ^^]

2 Likes

Not to worry - I added it as a Property and everything now works :slight_smile:

1 Like

On to multidimensional arrays -

So I create a Property with books()
But what if I want a 3 dimensional array?
Say for example I need a 3 dim array with upper bounds of books(2000,50,20)

Mark,

not a direct answer to your questions, but Xojo is Object Oriented Programming all the way, event an integer variable is an object. VB6 was not OOP.

I suggest you get familiar with OOP, find an introduction on the Web. You will the learn what are classes, methods properties an events. Then get back to working with Xojo. Note that not all languages implements all of OOP, for example Xojo does not implement multiple inheritance. You will find Xojo a very good system.

2 Likes

And there is no “on error resume next”.

3 Likes

Well that’s the end of the world right there haha ^^
No goto 3 either I guess.

Thanks Gilles
You will have to forgive me, but I’m an extremely impatient coder. Typically most of my small to medium apps are done in 1-3 days. My very large apps take months, and even with those I’ve coded 4 days straight with 4 hours of sleep total for those days. The world starts to shake around terribly so it’s not my normal practice. I try to get a regular 8 hours even though I wear a Hauwei eband which can’t be conned by semi-awake sleeping.

Where did you got this info from? Friendly question :wink:

2 Likes

Yeah, sort of had the same question.

same as xojo it is oop but not 100%.

2 Likes

Value types are not objects, though they may appear to be thanks to having extension methods.
https://documentation.xojo.com/getting_started/using_the_xojo_language/data_types.html

2 Likes

Do you know what Class is ?
Do you know how to use them ?

great.
some essentials in xojo you should look at:
container controls and find the inspector behavior menu.
event devinition.
create subclass and to know what is the super property.
class and class interface.
control set.
difference between property and computed property.
what is a shared method or shared property.
using System.DebugLog.

1 Like

I don’t use classes in my VB6 apps. I do have a sense as to what they are - function blueprints, or a sort of portion of a factory to which you can send data to for limited processing. I’m doing a course on C# and they have that in there where you can send variables for processing but they don’t have to adhere to the name but rather the type.

Thanks. Noted. Will proceed to action this to-study list.
One big plus that I’ve seen with Xojo is the tabs. Wish VB6 had that.
Currently I’m using VB6 and scrolling doesn’t work. It used to with a mouse app, but Win 10 has since unrecognised VB6 as a valid app so the mouse scrolling is broken.
Now I take note of the line numbers in the taskbar so I can jump to specific areas or double click on controls. It’s a bit painful.

I’ve looked at containers already - they’re great. Reminds me of iframes for websites. I remember when iframes were introduced that I used them to death and they still work thankfully.
Also like the keyboard shortcuts - Ctrl I L E ,etc. very useful.

Can anyone help me with multi-dimensional arrays?

I need to create a 3 dimensional array as a Property with upper bounds of variable(2000,50,20)
Not sure how to do this.

what is the meaning of 2000,50,20
usually u not need a fix ubound.
and i guess you get a better solution with classes.

2000,50,20 are the number of variables which will go into each array position.

Need to know how to declare a 3 dimensional array.