ASP.NET Basics (part 2): Not My Type - Cast And Credits
(Page 8 of 10 )
There are two kinds of casting supported by C# - "implicit casting" and "explicit casting".
Implicit casting means that the compiler will convert the variable from one data type to another data type on its own. However, this is possible only when the numeric range of the target type is greater than the numeric range of the source type.
Confused? Take a look at this next snippet:
<%
// price
short price = 1000;
// discounted price
int discountprice = price - 100;
%>
In this case, I have defined two variables of different data types - "short" and "int". The range of values that can be stored in the "price" variable ("short" data type) is a subset of the values that can be stored in the "discountprice" variable (of "int" data type). Therefore, when performing a mathematical operation like the one above, the compiler will carry out an implicit cast and convert the "price" variable into an integer.
How about the other way around?
<%
// price
int price = 1000;
// discounted price
short discountprice = price - 100;
%>
If you try running this, you'll see the following error:

You can view the possible combinations of implicit casting carried out by the .NET compiler at http://www.asia.cnet.com/builder/program/windows/0,39009376,39099752,00.htm
You've already seen implicit casting in this article, a few pages ago - although you might not have noticed it. Remember the following lines of code?
<%
intSbyte.Text = "" + varSbyte + "";
%>
By default, only a string can be assigned to the "Text" attribute of a "label" server control. Since "varSbyte" is an integer, it cannot be assigned directly; I first need to convert it into a string. The compiler will implicitly cast it into a string (and resolve the problem for me) if I concatenate it with any other string value....hence the string concatenation operations in the code above.
If this seems complicated to you - it is. Keep reading, I'll show you a simpler way to convert numbers to strings on the next page.
Now for explicit casting.
Explicit casting, as the name suggests, allows you to forcibly cast a variable from one data type to another. Here's a variant of a previous example that illustrates what I mean:
<%
// snip
// discount amount on each item - should equal 13.2
double discountamt = (double) (price * discountrate) / 100;
// grand total for all items - should equal 627
double grandtotal = (price - discountamt) * numitems;
%>
If you re-run the example with the changes above, this is what you'll see:

Now, the output shows correct values for the discount as well as the grand total. This is because I changed the data type of the "discountamt" and "grandtotal" variables to "double" so that they can store decimal values accurately. However, since "price" and "discountrate" are still integers, their product will also be an integer. Consequently, the division of this value by 100 will also be an integer value with the fractional part being lost. To avoid this loss, I have explicitly cast the result of the multiplication operation as a "double" - this will ensure that the correct decimal value (13.2) will be stored in "discountamt" instead of the incorrect integer value (13).
Next: Mixing It Up >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire