[thelist] Site Check - IE Explorer v5 anomaly
Ian Anderson
ian at zstudio.co.uk
Wed Oct 26 16:11:49 CDT 2005
Ian Anderson wrote:
> Anyway - apologies to Fred for not contributing anything useful to his
> question - tip on its way
<tip type="ASP/VBScript" author="Ian Anderson">
It's easy to be too complacent relying on the ability of scripting
languages to perform implicit type conversion when using comparison
operators. I was bitten by this yesterday when comparing the values of
two numbers where one of them came off a querystring.
In the code I was testing, the expression
If foo < bar Then
evaluated to *true* when both foo and bar contained 2!
It was only after much head scratching that I realised that the variant
coming off the querystring had a subtype of string, and forcing it to
integer with CInt fixed the apparent anomaly.
What I had forgotten was that in VBScript when you compare the values of
two variants with comparison operators, an integer subtype always
compares less than a string subtype - whereas in other languages (e.g.
JavaScript), you might expect the implicit type conversion of the <
operator to convert the string to an integer before doing the comparison.
So, note that in VBScript:
2 < 2 ' this is false
2 < "2" ' this is false
bar = "2"
2 < bar ' this is false
foo = 2
bar = "2"
foo < bar ' this is true
Use TypeName(foo) when you need to return the variant subtype for
troubleshooting purposes.
</tip>
Cheers
ian
--
_________________________________________________
zStudio - Web development and accessibility
http://zStudio.co.uk
Snippetz.net BETA - Online code library
File, manage and re-use your code snippets online
http://snippetz.net
More information about the thelist
mailing list