Data type suffixes are a way of identifying what data type a variable is. Each data type is represented by a different suffix character which is added to the end of a variable name. For instance, the character % represents the data type integer. Whenever a variable name has a % at the end of the name, you know the data type for that variable is integer.
When data type suffixes are used, the chance of creating a data type mismatch error is greatly decreased. (A data type mismatch error is a particularly annoying error all programmers run into at some point in time.) One case in point would be if you tried to add 1 to a variable named AGE. Earlier in your program, you had declared AGE as type string, but had forgotten that. When your program is executed, the statement AGE + 1 is executed, creating a data type mismatch error, and causing the program to abruptly end. However, if you had named the variable AGE$, ($ being the suffix for the string data type) you would not have forgotten that AGE$ was of type string, and the error could have been avoided.
Previously, the DIM statement was described as:
Although there is nothing wrong with the statement, it can be enhanced using data type suffixes. The following statement accomplishes exactly the same thing as the DIM AGE as Integer statement above.
This new version of the DIM statement declares that the variable AGE is of type integer because the % character represents the integer data type. Below is a listing of all the data types available with their suffixes.
Data Type | Suffix | Standard Declaration | Suffix Declaration |
Integer | % | DIM Age As Integer | DIM Age% |
Long | & | DIM Rad As Long | DIM Rad& |
Single | ! | DIM Balance As Single | DIM Balance! |
Double | # | DIM Amort As Double | DIM Amort# |
Currency | @ | DIM Deposit As Currency | DIM Deposit@ |
String | $ | DIM LName As String | DIM LName$ |
Variant | NO SUFFIX AVAILABLE | DIM X As Variant | DIM X |
Sub Click(Source As Button)
Dim LName$ 'Declare LName$ as type String
Dim Age% 'Declare Age% as type Integer
Dim CheckIt 'Declare CheckIt as type Variant
LName$ = Inputbox$("Please enter your name.") 'Ask for users name
CheckIt = Inputbox("Please enter your age.") 'Ask for users age
While Not Isnumeric(CheckIt) ' While the age is not a valid number
CheckIt = Inputbox("Please enter your age. Use only numbers!") 'Ask for the age until the age is a valid number
Wend 'End While loop
Age% = CheckIt + 1 'Assign Age the input age plus 1
Messagebox(LName + ", you will be " + Cstr(AGE%) + " on your next birthday!") 'Display a message to the user
End Sub
Click here to view the example.