ASP.NET Code
  Home arrow ASP.NET Code arrow Page 9 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 33
    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


    ASP.NET Basics (part 2): Not My Type - Mixing It Up


    (Page 9 of 10 )

    Thus far, I have only converted one numeric data type to another. How about converting a string to a number?

    C# comes with an entire range of functions to convert a string to any other data type - here's a quick example that demonstrates:


    <script language="c#" runat="server">
    void Page_Load()
    {

    // string to convert to number type
    string str"123";

    // convert to a int integer
    int varInt Convert.ToInt32(str);

    // set the labels 
    strnumber.Text str;
    intInt.Text varInt.ToString();


    </script>
    <html>
    <head><title>Strings At Play</title></head>
    <body>
    The string "<asp:label id="strnumber" runat="server" />"... converted to a "int" <asp:label id="intInt" runat="server" />.
     </body> </html> 

    And the output in the browser:

    This isn't all - you can also convert a string to any other number type, using the following functions:

    Convert.ToSByte(str) - convert str to a sbyte integer

    Convert.ToInt16(str) - convert to a short integer variable

    Convert.ToInt32(str) - convert to a regular integer variable

    Convert.ToInt64(str) - convert to a long integer variable

    Convert.ToByte(str) - convert to a byte integer variable

    Convert.ToUInt16(str) - convert to an unsigned short integer variable

    Convert.ToUInt32(str) - convert to a unsigned integer variable

    Convert.ToUInt64(str) - convert to a unsigned long integer variable

    What if the string can't be converted to a number, as in the following example?


    <script language="c#" runat="server">
    void Page_Load()
    {

    // string to convert to number type
    string str"abc";

    // convert to a int integer
    int varInt Convert.ToInt32(str); 

    // set the labels 
    strnumber.Text str;
    intInt.Text varInt.ToString();


    </script>
    <html>
    <head><title>Strings At Play</title></head>
    <body>
    The string "<asp:label id="strnumber" runat="server" />"... converted to a "int" <asp:label id="intInt" runat="server" />.
     </body> </html> 

    What will C# do?

    You can also do the reverse - convert a number into a string - by means of the ToString() function, demonstrated below:


    <%

    // snip 

    // Set the labels 
    strnumber.Text str;
    intInt.Text varInt.ToString();

    // snip

    %>

    Cool, huh?

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek