Converting between Data Types in VB.net

By: Steven Holzner Viewed: 153327 times  Printer Friendly Format    


Take a look at this code:

Option Strict On
Module Module1
    Sub Main()
        Dim dblData As Double
        Dim intData As Integer
        dblData = 3.14159
        intData = dblData
        System.Console.WriteLine("intData = " & Str(intData))
    End Sub

End Module
Tip 

Note how I'm using WriteLine to display text and the value in a variable by passing it the expression "intData =" & Str(intData). You can also embed codes like {0}, {1}, and so on into a text string, which will then be replaced by successive values passed to WriteLine. For example, this code: System.Console.WriteLine("The time is: {0} hours {1} minutes", 10, 2) displays the text "The time is: 10 hours 2 minutes".

In this case, I've turned Option Strict on, which means that Visual Basic will not automatically convert data types when you assign a value of one type to a variable of another, so it'll have problems with the statement highlighted above, where I assign a double precision floating point variable to an integer variable. To fix this problem, I have to do a specific type conversion. I do this with the CInt function, which converts its argument to type Integer:

Option Strict On
Module Module1
    Sub Main()
        Dim dblData As Double
        Dim intData As Integer
        dblData = 3.14159
        intData = CInt(dblData)
        System.Console.WriteLine("intData = " & Str(intData))
    End Sub

End Module

When I run this code, I get this result—notice that the decimal places have been removed to make the value of pi into an integer:

intData =  3
Press any key to continue

Here's the list of conversion functions you can use:

  • CBool— Convert to Bool data type.

  • CByte— Convert to Byte data type.

  • CChar— Convert to Char data type.

  • CDate— Convert to Date data type.

  • CDbl— Convert to Double data type.

  • CDec— Convert to Decimal data type.

  • CInt— Convert to Int data type.

  • CLng— Convert to Long data type.

  • CObj— Convert to Object type.

  • CShort— Convert to Short data type.

  • CSng— Convert to Single data type.

  • CStr— Convert to String type.

If you can't remember the name of a particular conversion function, you also can use the CType function, which lets you specify a type to convert to. (This is useful if you're converting to a type that is not one of the simple types in the list above.):

Option Strict On
Module Module1
    Sub Main()
        Dim dblData As Double
        Dim intData As Integer
        dblData = 3.14159
        intData = CType(dblData, Integer)
        System.Console.WriteLine("intData = " & Str(intData))
    End Sub
End Module
Tip 

CType is compiled in-line, meaning the conversion code is part of the code that evaluates the expression. Execution is faster because there is no call to a procedure to perform the conversion.

Visual Basic supports a number of ways of converting from one type of variable to another—in fact, that's one of the strengths of the language. You can also use the conversion statements and procedures that appear in Table below.

Visual Basic data conversion functions.

To convert

Use this

Character code to character

Chr

String to lowercase or uppercase

Format, LCase, UCase, String.ToUpper, String.ToLower, String.Format

Date to a number

DateSerial, DateValue

Decimal number to other bases

Hex, Oct

Number to string

Format, Str

One data type to another

CBool, CByte, CDate, CDbl, CDec, CInt, CLng, CObj, CSng, CShort, CStr, Fix, Int

Character to character code

Asc

String to number

Val

Time to serial number

TimeSerial, TimeValue



Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial