pkailas said:
an empty string tests true in IsNumeric(emptystring) and is not the same as ""
Hmmm...you may want to test that theory.
I agree that an empty string tests as true in iNumeric, which is why I placed the empty string test in that line as well, with a logical AND so that both would have to be true to pass that first line.
However, "" is an empty string. If you had, perhaps, meant to say that an empty variable is not the same as "", you would have ben more correct, though the difference between an empty variable and an empty string is a non issue if you are testing for <> "".
Any time you receive back an uninitialized variable in VBScript or declare a variable without providing it with a value, you get a reference to a value of type vbEmpty. When VBScript attempts to compare two variables of differant types it will first attempt to cast them to the same type, since all variables are basically variants in VBScript. When attempting to cast a variable of type vbEmpty, there are several default values, depending on the type it is being cast to. The default for numbers is 0, the default for booleans is false, and the default for strings is a 0-length string (ie, "" or what we like to call an empty string), etc.
Therefore, by testing the value against an empty string (ie, myvar <> "") we not only make sure we aren't dealing with a string, we also make sure we aren't dealing with an empty value, since the empty value will be cast to a string during comparison. An example of the difference between these two situations can be shown using Request.QueryString:
1) Empty string from Request collection:
If your URL was:
Then Request.QueryString("a") should be an empty string, but still a vbString type (not vbEmpty)
2) Empty variable form Request collection
If your URL was:
Then Request.QueryString("a" should be an empty variable, but can be cast to an empty string.
To further display this point, here is an example. I added a case statement from another piece of code to convert the varType constants to text for easier reading:
Code:
<%
Dim a(5), i
a(0) = 1
a(1) = "two"
a(2) = "10101"
a(3) = "-10101"
a(4) = ""
For i = 0 to 5
Response.Write "A(" & i & ") is: " & VariableToString(a(i)) & "<br>"
Response.Write " isPosNumeric(A(" & i & ")) :: " & isPosNumeric(a(i)) & "<br>"
Response.Write " A(" & i & ") = "" :: " & (a(i) = "") & "<br>"
Response.Write " isNumeric(A(" & i & ")) :: " & isNumeric(a(i)) & "<br><br>"
Next
Function isPosNumeric(aStr)
isPosNumeric = (aStr <> "") And isNumeric(aStr)
If isPosNumeric Then isPosNumeric = (cInt(aStr) >= 0)
End Function
Public Function VariableToString(aVar)
Select Case VarType(aVar)
Case 0 'Empty
VariableToString = "[Empty]"
Case 1 'Null
VariableToString = "[Null]"
Case 2 'Integer
VariableToString = "[Integer] " & aVar
Case 3 'Long
VariableToString = "[Long] " & aVar
Case 4 'Single
VariableToString = "[Single] " & aVar
Case 5 'Double
VariableToString = "[Double] " & aVar
Case 6 'Currency
VariableToString = "[Currency] " & aVar
Case 7 'Date
VariableToString = "[Date] #" & aVar & "#"
Case 8 'String
VariableToString = "[String] """ & aVar & """"
Case 9 'Object
VariableToString = "[Object]"
Case 10 'Error
VariableToString = "[Error]"
Case 11 'Boolean
VariableToString = "[Boolean] " & aVar
Case 12 'Variant
VariableToString = "[Variant]"
Case 13 'DataObject
VariableToString = "[DataObject]"
Case 17 'Byte
VariableToString = "[Byte]"
Case Else '8192+ - Array
VariableToString = "[Array] Size " & UBound(aVar)
End Select
End Function
%>
As you will notice, a variable of type vbEmpty will indeed pass the equality check against empty string. And the function will return false for that value, despite the fact that it's value can be cast to 0 during an isNumeric() test and when using functions like cInt().
-T