Subtype Change

Din lngAge
lngAge = InputBox("Plese enter your age in years")
MsgBox "TypeName After InputBox: " & TypeName(lngAge)
If IsNumeric(lngAge) Then
lngAge = lngAge + 50
MsgBox "TypeName after Adding 50: " &TypeName(lngAge)
MsgBox "In 50 years you will be " & lngAge & "years old."
Else
MsgBox "Sorry, but you did not enter a valid number."
End If

While you have to aware of implicit type coercion, there is no reason to fear it. VBScript is not going to arbitrarily go around changing subtypes on you. Implicit type coercion only happens when you assign a new value to a variable that does not fit the current subtype. Generally, once a Variant variable has a subtype (based on the value first placed within it, or based on a subtype that your code explicitly coerced), it will keep that subtype as you place new value in the variable.

Where do you need to watch out for implicit type coercion is when you are dealing with a mixture of data types. I saw this in the example: when the data comes back from the InputBox ( ) function, it was a string. Then we did soem math on it, which turned it into a number. Give this code a try:

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)

First
Next