Understanding Variables in VBScript - Variable Names and Scope
(Page 2 of 4 )
The naming conventions in VBScript are pretty straightforward. Variable names may not be longer than 256 characters and may not contain spaces. They must consist only of alphanumeric characters and underscores. The first character in a variable name cannot be a number.
Unlike JavaScript, variable names in VBScript are case insensitive. Their name should be representative of the data that they contain. For instance, if you have a variable that represents a client’s name, you would be better off naming it "clientname," rather than something obscure like "n1." It’s also good practice to use the recommended naming conventions, although it is not required by the interpreter.
According to the naming conventions, a variable name should begin with an abbreviation that represents the intended data type for its value. Parts of the name should also be capitalized for readability. Following the naming conventions, the variable in the last example should be named "strClientName," since it most likely would reference a string containing a client’s name. The table below lists the data subtypes in VBScript along with their abbreviations.
Subtype | Abbr | Example |
Boolean | bln | blnFound |
Byte | byt | bytRasterData |
Date (Time) | dtm | dtmStart |
Double | dbl | dblTolerance |
Error | err | errOrderNum |
Integer | int | intQuantity |
Long | lng | lngDistance |
Object | obj | objCurrent |
Single | sng | sngAverage |
String | str | strFirstName |
While this isn’t part of the official naming conventions, another common practice is to name object variables with prefixes that are indicative of the object’s type. The table below shows a list of common prefixes that I often use in my code examples.
Object Type | Abbr | Example |
Array | arr | arrNames |
Collection | col | colItems |
Dictionary | dict | dictStateAbbr |
External Object | obj | objFso |
According to the naming conventions, variables should also be named according to their scope. A variable’s scope is the area within a script or program from which the variable may be safely referenced. This is determined by where the variable is initialized.
A variable’s scope may be either procedure-level or script-level (sometimes referred to as global). A procedure level variable is initialized within a procedure, such as a function, subroutine, or event. Script-level variables may be initialized anywhere outside of a procedure.
Attempting to reference a variable outside of its scope will result in a runtime error.
The naming conventions suggest adding an "s" prefix to any script-level variable. Procedure-level variables do not change. I’ve provided a simple example below.
sstrScriptLevel = "A script level variable."
'The following line results in a runtime error because the variable
'is out of scope.
WScript.Echo strProcedureLevel
Sub MySub
strProcedureLevel = "A procedure level variable."
' The following line is completely valid.
WScript.Echo sstrScriptLevel
End Sub
By now you may be wondering why you should bother with the naming conventions at all, if they’re not required by the interpreter. The naming conventions were developed as a way to help multiple programmers working on the same project, or to help a single developer with a large project.
By using naming conventions, you can tell at a glance what data type a variable will provide and what its scope is. This is especially useful if you are reworking someone else’s code or updating code you may have written in the past.
Next: Initializing and Debugging Variables >>
More BrainDump Articles
More By Nilpo