7
votes

I'm new to classic asp, all my experience is in c# .net and ColdFusion 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?

4
here is a bit of fun. Add sub can't( - Brian White
Add sub cint(Val) cint = clng(Val) end sub. Sorry about earlier post - Brian White

4 Answers

11
votes

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.

2
votes

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.

0
votes

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.

0
votes

If you CInt a null value you will get an error. This is not the case for Cstr

Assuming that checking myVar > 0 is basically a null check.

myVar=cstr(myVar)
If (not length(myVar) = 0) Then
    myNewCar = cint(myVar)
End If

So myNewCar would either be "" or the value and you'll never get an error