Variables and Constants
From Xojo Documentation
A computer has two types of memory: temporary and permanent. Temporary memory is used to remember things for as long as the computer is on. As soon as the computer is turned off (or restarted), anything in temporary memory is lost. This is often referred to as RAM (Random Access Memory). Permanent memory is remembered even after a computer is turned off. This type of memory is also called the storage and usually a hard disk, solid-state disk (SSD) or flash memory. You save things to permanent memory using files, which are discussed in the File Management section.
When you are writing code, you often need to remember things. You use a concept called Variables to store information in the program's temporary memory. Variables are great for holding information such as counter values, field values and anything else you need while your program is running. The term “variable” is used because the value that it contains can be changed by your program, so its value can vary.
A Constant is similar to a variable, except that once its value is set it cannot be changed by your program (so it is constantly the same).
A variable is the simplest way to store values. All variables are defined in your code using the Dim statement and each variable must be defined before it is used. Xojo is strongly typed: when you define a variable you tell it the type of data that it can contain.
A variable definition consists of four parts: the Dim keyword, the variable name, the As keyword and the data type. This is also often referred to as a "variable declaration". Here is an example variable declaration:
Once you have declared a variable, you can assign it a value:
Variables can be declared anywhere you can write code (such as a methods or events), but the declaration must precede its first usage.
If you have several variables of the same type, you can declare them all with one Dim statement:
If you want to declare variables of different types, you can also declare them in one Dim statement. For example, the following Dim statement is valid:
But in general it is considered better form to declare variables of different types on separate lines.
When you create a variable with the Dim statement you can also assign it a value in one step. You do so by following its data type with an equals sign and the value. For example:
You can also mix variables with and without initial values, as in:
Variables that are declared without an explicit value get the default value for the type. For example, the default value for Integer (and other number types) is 0. So in the above example, the variable a is declared as an Integer but is not assigned a value and gets the default of 0. If you examine the value of a, it will be zero. The variable b, on the other hand, gets the value of 15.
Notice also that the following is valid:
This statement declares three Integer variables and assigns all of them the value of 15. Although this is valid, you should be careful about assigning values to more than one variable in a single Dim statement because you could easily lose track of the assignments, which might lead to subtle bugs in your code.
Accessing a Variable
A variable can be accessed only from where it was declared (such as the method or event). When the method is finished, the memory that was used to store the variable’s value becomes available for other uses. This means that another method in the application cannot access the variable value.
The term scope is used to describe where something, such as a variable, can be accessed. Variables and constants declared within methods have what is called a local scope because they are locally available only to the method. Thus these are also referred to as local variables.
If you declare a variable or constant inside of a code block (such as an If statement, Select statement or any looping statement), its scope is local to the code block and not the entire method. For example, you can write:
Dim y As Integer
y = 10
' y goes out of scope here; the variable name y can now be reused.
' It is redeclared as a string in the following If statement
If x < 5 Then
Dim y As Text
y = "hello"
You can also assign variables a value by using another variable. Doing this does not affect the original variable when you are using simple data types. When using the simple data types, the new variable gets a copy of the data in the original variable. Changing the new variable does not change the value in the original variable.
This behavior is different when working with objects. Learn more about object assignment in the Classes section.
This example creates a text variable, assigns it a value and then assigns its value to another text variable:
Dim s2 As String
s2 = s1 // s2 now contains "hello"
// Changing s2 does not affect s1
s2 = "world"
// s1 still contains "hello"
A constant behaves similarly to a variable with these significant differences:
- You use the Const keyword to declare the constant instead of the Dim keyword.
- You do not have to specify the type when declaring it, although it is allowed.
- Its value cannot be changed once it has been set. That is what makes it constant.
Here are some example constant declarations:
Constants can be assigned only a literal value, another constant value or the result of a constant expression.
Constants created in this manner are also local to the method or event in which they are declared and follow the same scoping rules described for variables. To create a constant that can be used more globally, add it to a Module or Class.
These naming rules apply to Variable and constant names, but also apply to the name of everything in Xojo including methods, modules and classes which you will learn about later.
- Names must start with an ASCII letter (A-Z) or (a-z)
- Names that start with an underscore are not recommended and not supported so may not work in future versions
- The remainder of the name can contain any of the following:
- alphanumeric ASCII characters (A-Z, a-z, 0-9)
- underscore (_)
- Any Unicode character with code point greater than 127
- Names can be any length
- Names are case-insensitive (age and Age are considered the same)
If you use an invalid character, you will get a compile error (usually reported as a Syntax Error) when you try to run the project. For names that are specified using the Inspector, you will get a prompt indicating that the name is invalid and the name will revert to its previous name.
You might also want to refer to the Coding Guidelines to understand how Xojo code is written and named throughout the documentation.