Converting between Data Types in VB.net

By: Steven Holzner Emailed: 1700 times Printed: 2204 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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


VB.net Home | All VB.net Tutorials | Latest VB.net Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Steven Holzner
Changes in Controls from VB6 to VB.net
Throwing a Custom Exception in VB.net
Throwing an Exception in VB.net
Using Finally in VB.net
Using Multiple Catch Statements in VB.net
Exception Filtering in the Catch Block in VB.net
Raising an Exception Intentionally in VB.net
Getting an Exception's Number and Description in VB.net
Using On Error GoTo 0 in VB.net
Using Resume Next and Resume Line in VB.net
Understanding Scope in VB.net
Properties in VB.net
Procedure Delegates in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net

More Tutorials in VB.net
Scope in VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures in VB.net
Creating Functions in VB.net
Passing a Variable Number of Arguments to Procedures in VB.net
Specifying Optional Arguments with default values in Procedures in VB.net
Preserving a Variable's Values between Procedure Calls in VB.net
Procedure Delegates in VB.net
Properties in VB.net
Understanding Scope in VB.net
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net

More Latest News
Most Viewed Articles (in VB.net )
Converting between Data Types in VB.net
Using Excel 2007 Chart in VB.NET 2005
Using Excel 2007 Chart VB.net Picture Box
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in VB.net
The For Loop in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
What is .NET Framework and the Common Language Runtime?
Debug and Release Versions in .NET
File Extensions Used in VB .NET
Assemblies, Solutions and Projects in VB .NET
The Option and Imports Statements in VB .NET
Visual Basic Statements
Data types in VB.net
Most Emailed Articles (in VB.net)
Multi threaded Socket Programming in VB.net
How to create an XML file in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in VB.net
The For Loop in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
What is .NET Framework and the Common Language Runtime?
Debug and Release Versions in .NET
File Extensions Used in VB .NET
Assemblies, Solutions and Projects in VB .NET
The Option and Imports Statements in VB .NET
Visual Basic Statements
Data types in VB.net
Converting between Data Types in VB.net