Converting between Data Types in VB.net
By: Steven Holzner in VB.net Tutorials on 2008-11-25
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.
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 |
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net
Structured Exception Handling in VB.net
Creating Sub Procedures 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
Throwing an Exception in VB.net
Comments