Q: What are those funny characters at the end of some variable names?
Occasionally, you may see variable names appended with a symbol. For example, you may see something like:
ErrorMessage$ = "Invalid value."
In this example, there's a dollar sign ($) at the end of the variable ErrorMessage. Such a character is called a type-declaration character .
A type-declaration character is an alternative way of indicating a variable's data type. Normally, when a variable is declared, the type is specified using the "As typename" syntax, such as the following:
Dim ErrorMessage As String
However, type-declaration characters can be used in place of the type name. An equivalent declaration to the above would be:
Dim ErrorMessage$
Both Dim statements have the exact same effect - ErrorMessage is declared as a variable of type String.
There are six type-declaration characters. The following table lists each type-declaration character, its corresponding data type and a pair of equivalent declarations - one using the type name and the other using the corresponding type-declaration character:
|
||||||||||||||||||||||||||||
Type-declaration characters can be used anywhere, not just in a Dim statement. They not only serve to declare a variable's type, but also to help the reader recognize a variable's type when it's used in the code. There are, however, other more widely used methods of identifying a variable's type - such as Hungarian notation or other similar naming conventions.
Like a few other features in Visual Basic, type-declaration characters were included in the language definition for backward-compatibility with older versions of BASIC. Personally, I do not recommend their use. I believe that with the advent of other, more widely used and recognized means of identifying a variable's type (such as the Hungarian notation mentioned earlier), the use of type-declaration characters is simply an outdated naming convention. But whichever naming convention you use, the most important thing is to be consistent.