The XojoScript language

This topic describes the language operators, data types, keywords and commands that can be used with XojoScript and IDE Scripting.

Note: You cannot use any built-in Xojo classes within XojoScript.

Operators

All the language Operators are supported. This includes numeric and alphanumeric operators (+, -, *, /, etc.), the logical operators (And, Not, Or, etc.) and the class operators (IsA, Me, Self, etc.).

All forms of comments are supported.

Data types

All the standard data types can be used as well as their corresponding methods.

Arrays can use any of these types.

Note

Although the Text data type is available, its related methods are not. You are better off using String in XojoScript.

Commands

All of the language commands are supported, with these exceptions:

  • Declare

  • GetTypeInfo

Classes

You can create a class using the Class...End Class commands. A new class can have properties, methods, and events. Here is an example of how you can define a class:

' Sample Class definition
Class NewClass
  ' Property declarations
  Var i As Integer
  Var c As Color
  ' Method declarations
  Sub myMethod (x As Integer, y As Integer)
    ' Method code goes here
  End Sub
End Class

A class can be subclassed from another class using the Inherits command.

Inheritance

Class inheritance is implemented with the "Inherits" command as shown below:

Class MyNewSubClass
  Inherits NewClass

  ' continue class definition
End Class

Use the Super keyword to call an overridden method on its superclass.

Events

Classes can define events using the Event command:

Class MyClass
  Event MyEvent(myParameter As DataType) As DataType
End Class

Methods can handle such events using the Handles command. For example:

Class TestClass
  Event TestEvent(value As String) As Boolean

  Sub Show()
    If Not TestEvent("Working!") Then
      Print("Nothing to print.")
    End If
  End Sub
End Class

Class TestSubclass
  Inherits TestClass

  Function TestEvent(value As String) As Boolean Handles TestEvent
    Print(value)

    Return False
  End Function

End Class

Var c As TestSubclass
c = New TestSubclass
c.Show

Print(&uA + "Done!")

Enumerations

Class can define enumerations as shown below:

Class Address
  Enum AddressTypes As UInt8
    Home
    Work
  End Enum

  Var AddressType As AddressTypes

End Class

Var personAddress As New Address

personAddress.AddressType = Address.AddressTypes.Home

Select Case personAddress.AddressType
Case Address.AddressTypes.Home
  Print "Home address"
Case Address.AddressTypes.Work
  Print "Work address"
End Select

Type casting

Type casting is supported. Use the desired type's name as a function whose only argument is the object reference you want to cast. If it fails, it will raise an IllegalCastException.

Operator overloading

Operator Overloading is supported for classes as shown below:

Class OneBasedArray
  Private mArray() As Variant

  Sub Constructor(bounds As Integer = 0)
    mArray.ResizeTo(bounds - 1)
  End Sub

  Function Count() As Integer
    Return mArray.LastIndex + 1
  End Function

  Sub Operator_Redim(newSize As Integer)
    mArray.ResizeTo(newSize - 1)
  End Sub

  Function Operator_Subscript(index As Integer) As Variant
    Return mArray(index - 1)
  End Function

  Sub Operator_Subscript(index As Integer, Assigns v As variant)
     mArray(index - 1) = v
  End Sub
End Class

Modules

You can create modules using the Module...End Module commands. For example:

Module Foo
  ' Property declarations
  Var bar As Integer
  ' Method declarations
  Sub Baz()
    bar = 42
  End Sub
End Module

Modules are similar to modules in your project, except that that properties are always protected and constants are not allowed. You can get the effect of a public property or constant by putting the Var or Const command outside of the module like this:

Var globalValue As Integer
Const globalConstant = "Hello"

Module Foo
  Var bar As Integer

  Sub Baz()
    bar = 42
  End Sub
End Module

Computed properties

You can add computed properties to classes and modules by using the Property...End Property commands:

Module Test
  ' Standard property declaration
  Var p As Integer = 1
  ' Computed property declaration
  Property TestProp As Integer
    Get
      Return p
    End Get
    Set
      p = value
    End Set
  End Property
End Module

Interfaces

Interfaces are declared by the Interface...End Interface command. Interface methods are declared with a Sub or Function line just as they would be declared inside a class, but they have no contents and need no End Sub or End Function line. Here is an example:

Interface MovableItem
  Sub Move(x As Integer, y As Integer)
  Function LocationX() As Integer
  Function LocationY() As Integer
End Interface

Function declarations with no parameters must have empty parentheses, as shown above.

A class may declare that it implements an interface with the Implements keyword:

Class Box
  Implements MovableItem
End Class

Classes can implement any number of interfaces, but they must provide all of the methods listed in the interface.

To implement more than one interface, each interface must be designated separately with the Implements keyword. For example, the class below implements MovableItem and ClickableItem:

Class Box
  Implements MovableItem
  Implements ClickableItem
End Class