|
||||||
Initially all VBscript variables are of the same type - the variant; however as soon as a value is assigned then they can become any one of 16 data types.
VBScript is different from many other programming languages in that it doesn't need to have variables defined as specific types - instead it used a variable type called a variant. A variant can contain any data type. That's not to say that the data type doesn't change - what actually happens is that VBScript changes the data type for the programmer automatically. So that raises the question - how does a programmer work with Variables in VBScipt? Defining a Variable in VBScriptVariables can be created with VBScript just by assigning a value to the variable, for example: v1 = 20
However, this is not considered good practice because errors can easily creep in without the programmer being aware of them: v1 = 20
v1 = vl * v1
msgbox v1 'Result is 0 not 400
A much better solution is to force variable declaration by using the option explicit statement: option explicit
var v1
Notice that the variable is defined without a data type, but the important point is that the following will now cause an error: v1 = vl * v1
Next the programmer can start thinking about the data types of the variables. Identifying the VBScript Variable TypesHaving said that the variables are all the same data type (variant), the data type does change as soon as a value is assigned to the variable, and the data type of a variable can be examined by using either of two VBScript methods:
For example: Dim x
x = 1
msgbox TypeName(x) 'Returns Integer
msgbox VarType(x) 'Returns 2
In fact, the data type can change continually through the running of the script: Dim v
msgbox TypeName(v) 'Returns Empty
v=1
msgbox TypeName(v) 'Returns Integer
v=1000000
msgbox TypeName(v) 'Returns Long
v=2.1
msgbox TypeName(v) 'Returns Double
v = "All Done"
msgbox TypeName(v) 'Returns String
Having seen that the data types do change it's worth looking at what those data types are. The VBScript Variable TypesThere are 16 data types used by VBScript and each is identified by:
It should be noted that the numbering for the VBScript constants is not consecutive: 0 Empty
1 Null
2 Integer
3 Long
4 Single
5 Double
6 Currency
7 Date
8 String
9 Object
10 Error
11 Boolean
12 Variant
13 DataObject
17 Byte
8192 Array
Each of the constant values can be used by prefixing the data type name with vb, for example: if VarType(v) = vbInteger then
msgbox "This is an integer"
end if
However, even though VBScript assigns all of the data type, it is still possible for the programmer to change them. Manually Changing the Data TypesVBScript will change the data type automatically as the script progresses; however, the programmer can manually change the data type if required. The conversion methods, and the data type that they convert to, are: cInt - Integer
cLng - Long
cSng - Single
cDbl - Double
cCur - Currency
cDate - Date
cStr - String
cBool - Boolean
cByte - Byte
So, for example: v = 2.1
v = cInt (v)
msgbox v 'Result is 2
And so the programmer has the choice to control the variable data types or to let VBScript do all of the hard work. SummaryVariables in VBScript are very liberal - they allow programmers to create variables with the data type variant and will the automatically assign the correct data type when a value is assigned to the variable; this data type can be queried by using either of two methods:
Of course once the data type has been assigned the programmer can manually change it if the script required it.
The copyright of the article Windows Scripting: VBScript Data Types in Windows Programming is owned by Mark Alexander Bain. Permission to republish Windows Scripting: VBScript Data Types in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||