Declaring and Use of Variables

Hi
I’m a Visual Basic 6 developer and just discovered and signed up to become a Xojo developer this week. I’m self taught.

Question 1
When declaring a variable - is ‘action’ or ‘open’ the preferred place to declare them?

Question 2
I’ve been experimenting and declared a variable in a control’s ‘open’ event handler. Under the ‘action’ event handler it says that ‘the item does not exist’ Are variable declarations so local that they are not shared by events?

Question 3
I added a pushbutton control to a window to toggle a dual panel mode and added the following code -

var vMode As Integer

vMode = vMode + 1
if vMode=2 then vMode=0

if vMode=1 then
me.caption=“Dual On”
Else
me.caption=“Dual Off”
End If

However the variable seems to be stuck on a value of 1

Question 4
When declaring / initialising a variable in an event handler, does it initialise every time the event handler is called or is the variable just declare once? Ddoes the value reset to it’s declared value every time the event handler is used?

Question 5
In Visual Basic I used a lot of sub routines to handle specific processes. With Xojo do I now use Methods. What’s the best practise for this?

Question 6
I have a lot of arrays and variables that need to be used by many controls, should these be declared in a Method as global variables? I have tried this but I get an ‘Item not found’ error.

This may help: Static - Xojo Documentation

Variables declared with the Var statement are completely local to the method and are destroyed when each invocation of the method goes out of scope.

When you declare a Var in ‘open’ it will get destroyed when ‘open’ finishes, it will not exist in ‘action’. That is why vMode is always 1, each time you push the button a new vMode is created.

1 Like

Excellent. Thanks!!!
Wow, why do all these tutorial videos use ‘var’ when ‘static’ is the declaration method most people will use for large apps? Starting people off on the wrong foot.
My toggle now works. Thanks. :slight_smile:

So my question now is that I want other controls to have access to the vMode variable. How do I make it global?

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://docs.xojo.com/UserGuide:Data_Types

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.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.