Variable is a name given to a data storage location such as memory that can change during program execution. It is a place in the memory where the data can be placed and retrieved during the course of execution.
VBScript has only one data type that is Variant. VBScript variables (Variant) can contain either string or numeric value. When a number is assigned to the variable it behaves as numeric and as a string when assigned a string value. There is no need to declare the type of variable before using it as in standard programming languages such as C, C++, Java, etc but if declared that makes the code neat and readable.
Variant has different subtypes such as:
- Empty – Not initialized
- Null – Contains no data
- Boolean – Value is either True or False
- Byte – Contains integer value from 0 to 255
- Integer – Holds integer value in the range of –32768 to +32767
- Currency
- Long
- Single – Single precision floating point number
- Double – Double precision floating point number
- Date / Time – Contains data / time
- String – Variable length string
- Object – Holds an object
- Error – Holds error
Though above are all the subtypes of variant there is no need to declare in the beginning and use them in the script.
Let us try to write a vbscript and understand the concept.
x=5647 y=-100 sum=x+y MsgBox sum If (sum>0) Then res="positive" Else res="negative" End If MsgBox res
In the above code although x,y,z, and sum are not declared as Integer data type VBScript treated them as integers and assigned appropriate subtype. Also, after assigning string value to res variable that has taken the role of String variable. This makes the users of VBScript more comfortable and concentrate on the actual task of writing the script.
Thanks for sharing.