Local variables are only available in the task where they have been declared. While declaring a variable, its type is set. The following types are available:
It is possible to build arrays from all those types.
A variable is declared by the Dim statement. Variable names must start with a letter and must not contain special characters. Here some examples:
Dim A1 As Integercorrect
Dim Ä1 As Integerincorrect
Dim 1A As Integerincorrect
Dim A 1 As Integerincorrect, space not allowed
Dim A_1 As Integercorrect
In the screenshot above, the Dim statement declares a String variable named foo. Then, the variable foo gets the value “Hallo Welt”. The content of foo is displayed by the ShowMessage command with foo as parameter. In this example, a line ShowMessage(“Hallo Welt”) would have the same result.
Variant variables are very special. Because they have no (or any) type, they are very flexible. A variant stores a value in different formats and “knows” which one is needed if there is an access to. In the example below a variant variable named v is declared:
For..Next loop, an integer value is assigned to v. As discussed above, the ShowMessage command need a string variable as parameter, but the line ShowMessage(“Count:” + v) is working correct, because v knows that only strings are valid and present its content as string. So v is used as integer and as string in the same subroutine. A more structured solution for the same job is the example below:
At first, an integer variable i is declared. The beginning of the
For..Next loop is unchanged, because the For..Next instruction expect an integer variable anyway. The command
ShowMessage(“Count:”+ i) will not work, because it is impossible to add an integer to a string. But V-Control Basic knows a function that converts an integer to string, the
Str(i) command. The result of str(i) is “10”, if i has the value 10. The string “10” comprising from ASCII code 49 (“1”) and ASCII code 48 (“0”).
An Overview of available basic functions is available in the Basic Language Reference Chapter.
Another often used variable type is boolean. Boolean variables know to states, True or False (1/0).
In this example, the variable b1 is declared as Boolean. The PromptMessage command need a boolean variable to return the result of the PromptMessage dialog. If the result is True, the OK button was pressed, if False, the user clicked the Cancel button. The if..then instruction check b1 and branch to the correct ShowMessage command.