Cast – Reassigning Variable Type

In ASP you might occasionally see error messages about casting a variable or object. To cast a variable is to reassign its type.

In other languages, like C or C++, there are very specific commands used in order to change a variable. If you had PiValue = 3.141519, and you just wanted the integer (3) part of that number, you would recast PiValue as an integer.

ASP is far less picky about its variables. You really don’t even have to define your variable types before you use them. If you have

PiValue = 3.141519

and you wanted to have a variable called PiShort with just the integer part, then you just say

PiShort = cInt(PiValue)

That’s it, it takes the integer of PiValue and puts it into PiShort.

If you end up with casting errors, do a bunch of response.writes to see EXACTLY what every variable is set to along the way.

For example, if you start setting up a mail object –

Set objMail = Server.CreateObject(“CDO.Message”)

and then you try to turn objMail into an integer, that won’t work 🙂 So make sure that the variable you are starting with can actually be cast into the destination type variable.

ASP Variable Basics