Coercing Variable for Integer Subtype

Dim varTest
varTest = 12
MsgBox TypeName(varTest)

Dim varTest
varTest = CInt("12")
MsgBox TypeName(varTest)

Dim varTest
varTest = "12"
varTest = CInt(varTest)
MsgBox TypeName(varTest)

All three of these examples achieve the same thing: coercing the varTest variable to have the Integer subtype. The first example results in the Integer subtype because we did not enclose the value 12 in quotes. This tells VBScript that we want the number to be treated as a number, not as text. The second example uses the CInt ( ) conversion function to transform the string value "12" into an Integer value before placing it in the variable. This tells the VBScript that we want the subtype to be Integer right from the start. The third example does the conversion after the fact. Any of these is a valid way to make sure that the value we are placing in the variable is treated as a numeric Integer value. However, the first example might be better because it is theoretically faster because we're not making the extra call to the CInt ( ).

Note that this code would be redundant:

Din varTest
varTest = CInt(12)

Because we don not have quotes around the 12, its subtype will automatically be Integer. However, this code has a different effect:

Din varTest
varTest = CLng(12)

This tells vbscript to make sure that the subtype of the variable is Long. The same numeric value of 12 is stored in the variable, but instead of being classified as an Integer, it is classified as a Long. This would be significant if you were passing the value to a VB/COM function that requires a Long.

First
Next