Data Types, declarations and scope
We have already seen that a variable is a name, that contains a reference to a value. In the above example we also saw that this value does not have to be a number, it could also be some text. This means that the variables have different data types.
Some programming languages require that you declare in advance what type of data a certain variable will contain. This is an optional feature in Sax Basic, but I would recommend that you always declare the data types of your variables. This should be done for a number of reasons:
- It is good programming style to declare variables.
- It makes the code easier to read for others.
- If you always declare variables, Sax Basic will tell you if you have misspelled a variable. This is the kind of error it takes hours to find.
- Your macro will take up less memory.
- Your code will run faster.
- Sax Basic will check if you are putting the right type of data into the variable.
Sax Basic has a variety of built in data types. We will just mention a few of the most widely used here. Look in the help files for a complete list of data types. A table of the most common data types follows:
| Data Type | Description | Range |
|---|---|---|
| Byte | 1-byte binary data | 0 to 255 |
| Integer | 2-byte integer | -32,768 to 32,767 |
| Long | 4-byte integer | -2,147,483,648 to 2,147,483,647 |
| Single | 4-byte floating point number | -3.4E38 to -1.4E-451.4E-45 to 3.4E38 |
| Double | 8-byte floating point number | -1.79E308 to -4.9E-3244.94E-324 to 1.79E308 |
| Currency | 8-byte number with fixed decimal point | 0 to approx 2 billion characters |
| Variant | Date/time, floating-point number, integer, string or object. 16 bytes, plus 1 byte for each character if the value is a string. | Jan 1 100 to December 31 9999,Numeric - same as doubleString: same as stringAlso can contain Null, False |
| Boolean | 2 bytes | True or False |
| Date | 8-byte date/time value | Jan 1 100 to December 31 9999 |
| Object | 4 bytes | Jan 1 100 to December 31 9999 |
| User-defined | dependent on definition |
To introduce a variable name in your code without any prior declaration of the variable, Sax Basic will automatically assume that this variable is of type Variant. This means that it can contain any type of data and Sax Basic will automatically change the type of the variable when needed. This may sound like a really good idea. You would not have to worry about data types and Sax Basic would do the work for you. In reality it is a very bad idea. In the beginning of your code, you do some calculations, and the results end up in a variable. You want to use this result in a later calculation, but at this later point, you make a typing error and the name of the variable is misspelled. Sax Basic would think you are introducing a new variable. Such a new variable would not contain any useful value and your macro would fail. Errors like that are very hard to find, and for that reason alone you should always declare your variables. If it is not clear to you why you should declare your variables, you can either just accept or go back and read the above section again.
The declaration of variables is done with the Dim statement. Dim is short for Dimension, dimensioning is another word for declaring. The following shows examples of variable declaration. The declaration must be made before the code where the variable is used.
Dim MyNumber As Integer
Dim theFloat As Double
Dim theName As String
Once the variable has been declared it can be used in the code, and Sax Basic will give you an error message if you try to put the wrong type of data into your variable. Constants are declared with a Const statement:
Const TranslationRate As Single = 50.3
Const WordCount As Integer = 25
You may wonder what constants are good for when you can't change their values. You might as well hard code the values into the macro. But consider this. You are building a macro to do some analysis on some translation lists.
In the calculations you might use certain constants like error codes. If this value is used many times it is not a good idea to hard code the value. If you want to use the macro for another analysis with another error count, you would have to change the value in a lot of places and you might forget some of them. Using constants also makes your code more readable.
If you don't tell Sax Basic otherwise, it will allow you to implicitly declare variables in your code. This means a new variable name in the code will automatically get the type Variant if it has not been declared. As mentioned this is a bad idea, so to force yourself to declare variables you should include the statement
Option Explicit
in the beginning of all modules (before declaring any Sub statements). This only effects new modules. I can highly recommend this setting.
You may already wonder if the variable keep their values even when the macro has ended. Usually they don't. The computer would eventually run out of memory if variables stayed in the memory. Variables are visible to a certain part of the Sax Basic environment. This part of the environment is known as the variables scope. Unless you tell Sax Basic differently the variable will disappear when the Sax Basic interpreter leaves the variable scope. There are in principle 3 different scopes in Sax Basic:
- Procedure
- Module
- Global
Variables with procedure level scope have their declaration within a procedure (a Sub or macro). They disappear when the macro ends. Module level scope variables are declared at the beginning of the module before any macro code. For instance right after the Option Explicit statement. They are visible to all macros in that module, and they will keep their values even after a certain macro has ended. Don't use these unless you absolutely need them, since they take up memory even when the macros have ended. Global or public variables are visible to all modules in the project, and should be declared with a Public statement at the module level:
Public WordCount As integer
You should also use these with caution or not at all.
A special case of procedure level variables is the static variable. They are declared in the procedure (or macro) and keep their value even after the macro has ended, but they are only visible to that procedure:
Sub myMacro()
Static WordCounter As integer
WordCounter = WordCounter + 1
End Sub
They are useful for keeping track of the number of times a certain macro has been called.