+ Reply to Thread
Results 1 to 4 of 4

Thread: Casting in VB.NET

  1. #1
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Casting in VB.NET

    Casting is the process of converting a value from one data type to another. This is often unnecessary by default in VB.NET, as Option Strict is disabled in the Visual Studio Express IDE. In C# it is enabled, but you should enable this in VB as well, from the Project Properties pages, to ensure you write the best code possible, with no possible loss of data, due to improper casting.

    In VB, you cannot just say, for example, a 'number'. You have to say a particular type of number. Does it have decimals? If not, then an Integer, Short or Long. If it has decimal places, then a Single, Double or Decimal, depending on the accuracy required.

    Mostly we don't need to dabble in converting different data types when writing our code, because we choose one that works and stick to it. However, some functions NEED a specific data type, and therefore we need to convert, or cast it, to the correct data type.

    Here is a reference list of all the conversion functions in VB.NET. Think of the letter C as standing for 'cast':

    • CBool(expression) - Boolean
    • CBytel(expression) - Byte
    • CChar(expression) - Char
    • CDate(expression) - Date
    • CDbl(expression) - Double
    • CDec(expression) - Decimal
    • CInt(expression) - Integer
    • CLng(expression) - Long
    • CObj(expression) - Object
    • CSByte(expression) - SByte
    • CShort(expression) - Short
    • CSng(expression) - Single
    • CStr(expression) - String
    • CUint(expression) - UInteger
    • CULong(expression) - ULong
    • CUShort(expression) - UShort

    Each function takes a single parameter - the data to cast - and returns a value of the type to which it is casting to. For example:

    [highlight=vb]
    Dim dblValue As Double = 35.35
    Dim sngValue As Single

    sngValue = CSng(dblValue)

    'Both variables now contain the same value.
    'Value of sngValue == Value of dblValue
    [/highlight]

    I hope this helps anyone who needs to convert between the data types in VB - casting is very common, especially to strings (using CStr), but remember that every object automatically inherits a ToString() method to convert the value to string, and that the data types have Parse() and TryParse() method in addition, which is very useful is you are using a .NET language other than VB.

    Please +rep if helpful. Leave comments here!
    Last edited by Xav; 07-29-2008 at 11:12 AM.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Casting in VB.NET

    I suppose the silence is a good thing...

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    Jordan Guest

    Re: Casting in VB.NET

    It takes me a while to find new tutorials sometimes. It isn't always the first section I check. When did you pick up VB.NET ?

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Casting in VB.NET

    Um, well, if you check Interview - Xav, it was my first language. I moved on to C# and then to web design, but it's still my first language. That was when I was 10 or 11, so maybe just over a year ago.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C# - Casting.
    By CommittedC0der in forum CSharp Tutorials
    Replies: 0
    Last Post: 08-26-2011, 01:13 PM
  2. about casting/downcasting
    By John Jang in forum Java Help
    Replies: 2
    Last Post: 05-16-2011, 04:24 AM
  3. casting
    By genux in forum C Tutorials
    Replies: 10
    Last Post: 02-13-2010, 12:30 AM
  4. Type Casting
    By clookid in forum PHP Tutorials
    Replies: 1
    Last Post: 01-11-2007, 06:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts