Xojo Fundamentals

Introduction

To introduce the Xojo language, the main features of the language are summarised in this lesson. This lesson should allow the user to add simple code to Event Handlers .

Variables

Xojo is a strongly typed language: variables must be declared before they can be used. The syntax to declare a variable is as follows:

Var variable name As data type

Variable names

The following rules apply to naming a variable:

  • Variable names must always start with a letter;
  • Variable names may contain letters, numbers and some special characters (for instance underscore (_));
  • Variable names in Xojo are case-insensitive (FirstName, firstname and FIRSTNAME refer to the same variable).
Use camel case (first letter is lowercase, subsequent words in the name start with a capital letter) to name variables (for instance firstName)

Data types

The following data types are frequently used:

Data typeDescriptionExample
IntegerInteger variables contain whole numbersVar score As Integer
score = 15
DoubleDouble variables contain decimal numbersVar salary As Double
salary = 4567.87
CurrencyFixed-decimal: mainly used for calculations with monetary dataVar bankBalance As Currency
bankBalance = 123678.78
StringString variables contain a series of charactersVar myName As String
myName = “John”
BooleanBoolean variables are limited to the values True or FalseVar invoicePaid As Boolean
invoicePaid = True
ColorColor variables contain color valuesVar highlight As Color
highlight = Color.Red
Data types

Mathematical operations

The following operators are used frequently:

OperatorOperationExample
+Addition5 + 7 = 12
Subtraction12 – 5 = 7
*Multiplication5 * 3 = 15
/Division22 / 4 = 5.5
^Exponentiation3 ^ 3 = 27
Mathematical operators

Complex mathematical expressions are evaluated using the following priority rules (evaluated from left to right):

  1. Parenthesis;
  2. Exponentiation;
  3. Multiplication and division;
  4. Addition and subtraction.

String operations

The + operator can be used to combine (concatenate) strings:

OperatorOperationExample
+Concatenation of strings“Napoleon ” + “Bonaparte” = “Napoleon Bonaparte”
String operator

Arrays

An array is a collection of data for a specific data type. An element in an array is accessible through its index. The index of the first element is zero (0). Arrays can be defined as follows:

Var arrayName() As dataType

Var arrayName(size1[,size2, size3...]) As dataType

Var arrayName As dataType = Array(value1, value2...)
Examples:

Var months() As String
months.Add("January")

Var months(12) As String
months(0) = "January"

Var months() As String = Array("January", "February", "March")

The following methods are very useful when editing arrays:

MethodDescriptionExample
AddAdds a new element at the end of the arraymonths.Add(“January”)
AddAtAdds a new element at the specified indexmonths.AddAt(2,”March”)
CountReturns the number of elements in an arraynumberElements = months.Count
FirstIndexReturns index of the first elementfirst = months.FirstIndex
LastIndexReturns index of the last elementlast = months.LastIndex
RemoveAllRemoves all elementsmonths.RemoveAll
RemoveAtRemoves the element at the specified indexmonths.RemoveAt(2)
SortSorts the array in ascending ordermonths.Sort
Array methods

Assignment statements

The assignment operator in Xojo is =. A variable to the left of the = sign gets a value from the right side of the statement. The value on the right side of the = sign can be a constant, a variable or an expression.

variable = constant|variable|expression
Examples:

age = 12
myAge = yourAge
totalPrice = netPrice + salesTax

Selection statements

A selection statement will select one or more other statements based on the evaluation of a condition. There are two selection statements available in Xojo: If…Then…Else and Select…Case.

A conditional expression evaluates whether a relation between two values is True or False. In most cases, relational operators will be used to define a condition. Xojo defines the following relational operators:

OperatorDefinition
=Equal
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to
<>Not equal
Relational operators

Logical operators can be used to combine two conditional expressions into one logical expression:

OperatorDefinition
AndAND (True if both conditions are True)
OrOR (True if any condition is True)
NotNot (inverts the result of a logical expression – If expression evaluates to TrueNot returns False. If expression evaluates to FalseNot returns True.)
Logical operators

If…Then…Else

The If…Then…Else statement has the following syntax:

If conditional expression1 Then
   statement(s)
ElseIf conditional expression2 Then
   statements(s)
ElseIf conditional expression3 Then
   ...
Else
   statement(s)
End If

ElseIf and Else statements are optional. If the first conditional expression is True then the statements following the Then keyword are executed. If the first conditional expression is False then the ElseIf condition (if present) will be evaluated. If none of the conditional expressions are True then the Else statements (if present) will be executed.

Example:

Var discountCode As Integer
Var discount As Integer

If discountCode = 1 Then
   discount = 5
ElseIf discountCode = 2
   discount = 10
Else
   discount = 0
End If

Select…Case

The Select…Case statement tests a variable for a possible value. When the variable matches one of the values ​​listed in a Case, the code following that Case keyword is executed. The Else sentence (no match was found) is optional.

The Select…Case statement has the following syntax:

Select Case variable
Case value1
   statement(s)
Case value2
   statement(s)
Case value3
   ...
Else
   statement(s)
End Select
Example:

Var discountCode As Integer
Var discount As Integer

Select Case discountCode
Case 1
   discount = 5
Case 2
   discount = 10
Else
   discount = 0
End Select

Iteration statements

Xojo has three statements to execute one or more lines of code multiple times: For…Next, While…Wend, Do…Loop.

For…Next

A For…Next statement is useful when you know how many times the iteration must run. The For…Next has the following syntax:

For variable = startValue To endValue Step stepValue
   statement(s)
Next

The Step clause is optional. startValue, endValue and stepValue can be a constant or a variable.

Example:

Var counterAs Integer
Var sum As Integer = 0

For counter = 1 To 10 Step 1
   sum = sum + counter
Next

While…Wend

A While…Wend statement will run the lines of code between the While and the Wend sentences as long as the conditional expression evaluates to True. If the condition evaluates to False the execution will continue with the code after the Wend sentence. If the condition is already False at the start of the iteration then the iteration instructions will not be executed. The While…Wend statement has the following syntax:

While conditional expression 
   statement(s)
Wend
Example:

Var counterAs Integer = 1
Var sum As Integer = 0

While counter <= 10
   sum = sum + counter
   counter = counter + 1
Wend

Do…Loop

The Do…Loop statement will execute all lines of code between the Do and the Loop keywords until the condition evaluates to True. The conditional expression can either be at the beginning or at the end of the Do…Loop. The Do…Loop statement has the following syntax:

Do Until conditional expression
   statement(s)
Loop

Or

Do
   statement(s)
Loop Until conditional expression
Example:

Var counterAs Integer = 1
Var sum As Integer = 0

Do Until counter > 10
   sum = sum + counter
   counter = counter + 1
Loop