Skip to content

Basic Syntax

Overview

The Basic syntax supports:

  • sub .. end and function .. end declarations
  • byref and dim directives
  • if .. then .. else .. end constructor
  • for .. to .. step .. next constructor
  • do .. while .. loop and do .. loop .. while constructors
  • do .. until .. loop and do .. loop .. until constructors
  • ^,*,/,and,+,-,or,<>,>=,<=,=,>,<,div,mod,xor,shl,shr operators
  • try .. except and try .. finally blocks
  • try .. catch .. end try and try .. finally .. end try blocks
  • select case .. end select constructor
  • [1,2,3] array constructors
  • exit statement
  • ObjectName.Property access to object properties and methods

Script Structure

The script structure consists of two major blocks:

  • Function and procedure (sub) declarations
  • Main Block

Both are optional, but at least one should be present in script.

Example 1:

vb
sub DoSomething
  CallSomething
end sub
 
CallSomethingElse

Example 2:

vb
CallSomethingElse

Example 3:

vb
function MyFunction
  MyFunction = "OK"
end function

Like in normal Basic, statements in a single line can be separated by a colon (😃.

vb
A = 100:B = 200

Identifiers

Identifier names (variable, function, procedure...) follow the most common rules in Basic. They should begin with a character (a..z or A..Z), or _ and can be followed by alphanumeric chars or underscore (_). They cannot contain any other character or whitespace.

Valid identifiers:

vb
VarName
_Some
V1A2
_____Some____

Invalid identifiers:

vb
2Var
My Name
Some-more
This,is,not,valid

Assign statements

Assign statements (assign a value or expression result to a variable or object property) are built using =.

Examples:

vb
MyVar = 2
MyNumber = 5.678
MyText = "This" + " is a text".

Floats

Float values use a point (.) as decimal separator and no thousand separator.

Example:

vb
A = 1234.56

Strings

Strings (sequence of characters) are declared in Basic using the double quote (") character.

Example:

vb
A = "This is a text"
B = "Text" + " concat"

Comments

Comments can be inserted inside a script. A single quote character (') or rem can be used. Comment will finish at the end of line.

Examples:

vb
' This is a comment
A = 10

rem This is another comment
A = 20

' And this is a comment
' with two lines
A = 30

Variables

There is no need to declare variable types in a script. Thus, variables can be declared by just using the dim directive and its name. There is no need to declare variables. In this case, variables are implicitly declared.

Example 1:

vb
sub SetText
  dim S
  S = "Hello world!"
end sub

Example 2:

vb
DIM A
A = 0
A = A + 1

You can also declare global variables as private:

vb
private A
public B
B = 0
A = B + 1

Variables declared with DIM statement are public by default. Private variables are not accessible from other functions or sub declarations.

Variables can be default initialized:

vb
dim A = "Hello world"
dim B As Integer = 5

Indices

Strings, arrays and array properties can be indexed using square brackets. For example, if Str is a string variable, the expression Str[3] returns the third character in the string denoted by Str, while Str[I + 1] returns the character immediately after the one indexed by I.

Example:

vb
MyChar = MyStr[2]
MyStr[1] = "A"
MyArray[1,2] = 1234
Strings1.Line[2] = "Line in Strings1 object"

Arrays

To construct an array, square brackets can be used. and multi-index arrays can be constructed by nesting array constructors. The multi-index array can then be accessed by separating indexes using a comma (,). If a variable is a variant array, the script automatically supports indexing in that variable. Array indexes are 0 based.

Example:

vb
NewArray = [2,4,6,8]
Num = NewArray[1] 'Num receives "4"

MultiArray = [["green","red","blue"],["apple","orange","lemon"]]
Str = MultiArray[0,2] 'Str receives 'blue'
MultiArray[1,1] = "new orange"

If Statements

There are two forms of if statement: if...then..end if and the if...then...else..end if. Like normal Basic, if the if expression is true, the statements are executed. If there is an ELSE part and the expression is false, the statements after ELSE are executed.

Example:

vb
function Test(I, J)
  if J <> 0 then Result = I / J end if
  if J = 0 then Exit Function else Result = I / J end if
  if J <> 0 then
    Exit Function
  else
    Result = I / J
  end if
end function

If the if statement is in a single line, it does not need to be finished with end if.

vb
if J <> 0 then Result = I / J
if J = 0 then Exit else Result = I / J

While Statements

A while statement is used to repeat statements, while a control condition (expression) is evaluated as true. The control condition is evaluated before the statements. Hence, if the control condition is false at first iteration, the statement sequence is never executed. The while statement executes its constituent statement repeatedly, testing expression before each iteration. As long as expression returns True, execution continues.

Example:

vb
while (Data[I] <> X) I = I + 1 end while
while (I > 0)
  if Odd(I) then Z = Z * X end if
  X = Sqr(X)
end while

Loop Statements

A Script supports loop statements.

vb
do while expr statements loop
do until expr statements loop
do statements loop while expr
do statement loop until expr

Statements will be executed while expr is true, or until expr is true. If expr is before statements, then the control condition will be tested before the iteration. Otherwise, the control condition will be tested after the iteration.

Example:

vb
do
  K = I mod J
  I = J
  J = K
loop until J = 0

do until i >= 0 
  i = i + j
loop

do 
  k = i mod j
  i = j
  j = k
loop while j <> 0

do while i < 0 
  i = i + j
loop

For Statements

A script supports for statements with the following syntax:

for Counter = InitialValue to FinalValue step StepValue Statements next

The for statement sets the counter to the initialValue, repeats the execution of statements until "next" and increments the value of the counter by stepValue, until the counter reaches the finalValue. The step part is optional, and if omitted, the stepValue is considered to be 1.

Example 1:

vb
for c = 1 to 10 step 2
  a = a + c
next

Example 2:

vb
for i = a to b
 j = i ^ 2
 sum = sum + j
next

Select Case Statements

A script supports select case statements with the following syntax:

vb
select case SelectorExpression
  case CaseExpr1
    Statement1
    ...
  case CaseExprN
    StatementN
  case else
    ElseStatement
end select

If the SelectorExpression matches the result of one of the CaseExpr expressions, the respective statements will be executed. Otherwise, the ElseStatement will be executed. The else part of the case statement is optional.

Example:

vb
select case Fruit
  case "Lime" Color = "Green"
  case "Orange" Color = "Orange"
  case "apple" Color = "Red")
case else
  Color = "Black"
end select

Function and Sub Declaration

Declaration of functions and subs are similar to Basic.

To return a function value from a function, an implicitly declared variable which has the same name of the function, or an explicit Return statement can be used.

Example:

vb
function TodayAsString
  Return DateToStr(Date)
end function

function TodayAsString
  TodayAsString = DateToStr(Date)
end function

function Max(a, b)
  if a > b then
    Max = a
  else
    Max= B
  end if
end function

A Return statement can also be used to exit subs and functions prematurely.

Example:

vb
sub SetUppercase(Text)
  a = Uppercase(Text))
  return
  ' This line will be never reached
  b = "Another Text"
end sub

Parameters by reference can also be used, using the byref directive.

Example:

vb
sub SwapValues(byref A, B)
  dim TEMP
  TEMP = A
  A = B
  B = TEMP
end sub

You can also declare subs and functions as private or public using the following syntax:

vb
private sub Hello
end sub

public function Hello
end function

Subs and functions are public by default. Private subs and functions are not accessible from other scripts.