SunQuest
 
       ASP.NET Code
  Home arrow ASP.NET Code arrow Page 8 - ASP.NET Basics (part 2): Not My Type
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET CODE

ASP.NET Basics (part 2): Not My Type
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 27
    2003-07-30

    Table of Contents:
  • ASP.NET Basics (part 2): Not My Type
  • Building Blocks
  • A New Assignment
  • Strange Characters
  • Playing The Numbers
  • Operate With Caution
  • Everything Must Go
  • Cast And Credits
  • Mixing It Up
  • It's A Wrap

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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).

    More ASP.NET Code Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET CODE ARTICLES

    - How to Use the ListBox Control in ASP.NET 2.0
    - How to Load XML Documents in ASP.NET 2.0
    - DataGrid Code
    - ASP.NET Guestbook
    - User Controls and Client Side Scripting
    - ASP.NET Programming with Microsoft's AS...
    - ASP.NET Basics (part 3): Hard Choices
    - ASP.NET Basics (part 2): Not My Type
    - ASP.NET Basics (part 1): Nothing But .Net
    - Directory Tree Browser
    - How to get the confirmation of Yes/No from a...
    - Complete example using custom errors and wri...
    - Paging Certain # records per page .NET style
    - General Methods of formatting and Subtractin...
    - .NET LinkButton web control





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway