Long Coerced into String

Dim lngTest
lngTest = CLng(100)
MsgBox "Type after initialization: " & TypeName(lngTest)
lngTest = lngTest + 1000
MsgBox "TypeName after adding 1000: " & TypeName(lngTest)
lngTest = lngTest * 50
MsgBox "TypeName after multiplying the 50: " & TypeName(lngTest)
lngTest = "Hello"
MsgBox "TypeName after assigning value of 'Hello': " & TypeName(lngTest)

If you run this code using Windows Script Host, you'll see that the first three calls to the TypeName ( ) function reveal that the subtype is Long. Then, after we changed the value of the variable to "Hello", the subtype is automatically coerced into String. What this code illustrates is that, once the subtype is established as Long, it stays Long as long as we keep changing the value to other numbers. VBScript has no reason to change it, because the values we put in it remain in the range of the Long subtype. However, when we place text in the variable, VBScript sees that the new value is not appropriate for the Long subtype, so it changes it to String.

First
Next