UserGuide

XojoScript Language

From Xojo Documentation

Revision as of 19:54, 15 February 2021 by Gperlman (talk | contribs) (See Also)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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, including as Boolean, Double, Integer, String, etc, as well as their corresponding methods.

Arrays can use any of these types.

fa-info-circle-32.png
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(EndOfLine + "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)
Redim mArray(bounds - 1)
End Sub

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

Sub Operator_Redim(newSize As Integer)
Redim mArray(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 simply 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 command:

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.

See Also

UserGuide:XojoScript topic