In the past I’ve cautioned you on always initializing your variables and encouraged you to always declare your variable types, rather than using the default variant type in VBScript. I’ll be the first to admit I don’t follow my own advice in my examples! However, variants can be dangerous!
Recently I had a situation where if statements were returning completely not correct results and for the life of me I couldn’t figure it out! Here is an example that illustrates what was happening to me.
one = 1
two = 2
three = 3
OneBillion = 1000000000
MyVar = inputbox(“Enter a number”)If one < MyVar Then
WScript.Echo (one & ” is lessthan ” & MyVar)
If two < MyVar Then
WScript.Echo (two & ” is lessthan ” & MyVar)
End IfIf three < MyVar Then
WScript.Echo (three & ” is lessthan ” & MyVar)
End IfIf OneBillion < MyVar Then
WScript.Echo (OneBillion & ” is lessthan ” & MyVar)
The basis of the problem is that I was taking input from a file that was a number– however I was reading the file and it was coming into the script and being used as a string by the variant variable. I then was comparing it to a number and the comparisons were not working 100% correctly. Check it out by entering several numbers into the script. Try entering 0, 1, 2, 3, and 1000000001.
The moral of the story here is to always cast your varables as a specific type or use the cint() function to convert your variant varables to numbers before doing number comparisons with a literal number and a variant variable that is supposed to contain a number.