Variables

Xojo Version 2021 r3.1

Description:

In programming a variable is a way we store a piece of data. Each variable has a name and a value.

Declaring Variables

Declaration statements give the variable it's name and allocates space in memory (RAM) to hold its value. In Xojo you must specify a data type when declaring a variable.

Syntax

Var variableName As data-type

Example

Var firstName As String


You can also declare multiple variables at one if they share the same data type

Var title, subtitle, description as String

Initializing Variables

When you store data into a variable it is considered initialized. By default all numeric variables are initialized to zero or zero length strings. It is considered good programming practice to always initialize your variables before you use them.

Syntax

Var variable-name As data-type = value

Example

Var itemName as String
Var itemSKU as Integer
Var itemPrice as Double

itemName = ""
itemSKU = 0
itemPrice = 0.0


You can also declare and initialize in one line

Var itemName as String = ""
Var itemSKU as Integer = 0
Var itemPrice as Double = 0.0

Assigning Data to a Variable

An assignment statement will tell the computer what data to store into a variable.

A = b + c
itemName = txtItem.text
itemSKU = val(txtSKU.text)