Re: Show if based on session var by OldPedant
OldPedant
Mon Jul 07 13:07:01 CDT 2008
> If both values are [convertible to] numeric values it is simple:
> <%
> if +Session("EMPLOYERUSERS") < +Session("EMPLOYERMAXUSERS") then
> %>
Ugh. I guess it works, but it hides the *INTENT* of the code.
Why not use the functions that are builtin just for this kind of purpose?
if CLNG(Session("EMPLOYERUSERS")) < CLNG(Session("EMPLOYERMAXUSERS")) then
Actually, using the + as Evertjan suggests is equivalent to using
CDBL( )
in both places. That's because when VBScript needs to convert a non-numeric
value to numeric, it just assumes that it should use the "widest" conversion
it knows of, which is convert-to-double. CDBL( ).
The number of byte code tokens generated (and that then have to be executed)
is identical, so the performance will be identical. Excepting that
conversion to integer via CLNG is just marginally faster as it doesn't have
to check for strings such as "3.1328E+27" and the like, as CDBL and the
implicit conversion of the + operator do.
Under the covers, VBScript accomplishes *ANY* of these using a call to the
COM function
variantChangeTypeEx( )
and that COM function has slightly different (and faster) code for
string-to-int than it does for string-to-floating-point. But we are talking
nanoseconds here, no matter what. So why not use the version that conveys
the *SENSE* of what you are trying to do, instead of relying on a hacky trick?