+ Reply to Thread
Results 1 to 4 of 4

Thread: Casting in VB.NET

  1. #1
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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 02:12 PM.

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

  2. #2
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  3. #3
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    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 ?

  4. #4
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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. does any1 know how to make a web browser in vb.net ?
    By MXTECH in forum Visual Basic Programming
    Replies: 6
    Last Post: 09-01-2008, 10:20 AM
  2. ALTER Queries to Access trough VB.NET
    By penkomitev in forum Visual Basic Programming
    Replies: 1
    Last Post: 02-04-2008, 01:06 PM
  3. VB vs VB.Net
    By Patrick in forum Visual Basic Programming
    Replies: 2
    Last Post: 10-14-2007, 12:58 PM
  4. VB.Net & VB 6.0 Syntax's
    By travy92 in forum Visual Basic Programming
    Replies: 10
    Last Post: 09-20-2007, 05:34 PM
  5. Need a program created in VB.Net
    By Thomas in forum MarketPlace
    Replies: 0
    Last Post: 08-29-2007, 07:02 PM

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