tags:

views:

178

answers:

3
+1  Q: 

classic asp cint

I'm new to classic asp, all my experience is in c# .net and cold fusion and php.

Anyway, this site I'm working on has this code all over the place

If (CInt("0" & myVar) > 0) Then
    myNewCar = CInt("0" & myVar)
End If

What I don't understand is why the "0" is append to the var in the cint() input? Am I just missing something? Is it some kind of safety thing? Is it efficient?

On a side note, any classic asp books recommended?

+1  A: 

myVar might be nothing, an object, empty string, or non-numeric. Pre-pending "0" guarantees you'll get some valid integer back out no matter what.

Joel Coehoorn
CInt(Empty) returns 0. An attempt to concat "0" with an object that does not have a default property would result in an error.
AnthonyWJones
+3  A: 

Its an old hack to handle null values. Calling CInt on a null would result in an error. However concatenating a string with a null results in the string hence "0" & null returns "0". This prevents CInt from erroring when the value is null.

AnthonyWJones
A: 

My classic ASP is a bit rusty, but I believe it's a safety thing. For instance, if myVar had not been defined and it tried to do a CInt, that would cause an error.

BBlake
With option explicit the code would not compile, without Option Explict an undefined variable is dynamically defined and given a value of empty. Empty values will CInt to 0.
AnthonyWJones