Data types in VB.NET
In this article I will explain your about the Methods to convert data types in VB.NET
.NET provide two ways to convert on data type to another in VB.NET.
- ToString and Parse method
- Convert class
ToString and Parse method
ToString method lets you convert any value to a string.
The Parse method is a shared method that performs the reverse operation of the ToString method.
It converts other a string value to another data type.
Practical demonstration of parsing
Module Module1
Sub Main()
Dim number As Integer
Dim weight As Decimal
Console.Write("Enter any number : ")
number = Integer.Parse(Console.ReadLine())
Console.Write("Enter your weight : ")
weight = Decimal.Parse(Console.ReadLine())
Console.WriteLine("You have entered : " & number)
Console.WriteLine("You weight is : " & weight)
Console.ReadLine()
End Sub
End Module
The following table shows Common methods for data conversion
Method
|
Description
|
toString
|
A method that converts the value to its equivalent string representation using the specified format. If the format is omitted, the value isn't formatted.
|
Parse
|
A Shared method that converts the specified string to an equivalent data value.
|
Convert Class
The Convert class provides shared methods that you can use to convert value with any data type to any other data type.
Practical demonstration of Convert class
Module Module1
Sub Main()
Dim num As String = "23"
Dim number As Integer = Convert.ToInt32(num)
Dim age As Integer = 45
Dim vote As String = Convert.ToString(age)
Console.WriteLine("Your number is : " & number)
Console.WriteLine("Your voting age is : " & age)
Console.ReadLine()
End Sub
End Module
When you use convert class, you should realize that the result of the conversion would vary depending on the type of conversion that you perform. For example if you try to a Decimal to an Integer the conversion will round the decimal digits.
Using this class can do most of the common conversions. Some of the shared methods of the Convert class:
Method
|
Description
|
ToBoolean
|
Overloaded. Converts a value to a Boolean value.
|
ToByte
|
Overloaded. Converts a value to an 8-bit unsigned integer.
|
ToChar
|
Overloaded. Converts a value to a unicode character.
|
ToDateTime
|
Overloaded. Converts a value to a DateTime.
|
ToDecimal
|
Overloaded. Converts a value to a Decimal.
|
ToDouble
|
Overloaded. Returns the specified parameter as an 8-byte Double precision floating-point number.
|
ToInt16
|
Overloaded. Converts a value to a 16-bit signed integer.
|
ToInt32
|
Overloaded. Converts a value to a 32-bit signed integer.
|
ToInt64
|
Overloaded. Converts a value to a 64-bit signed integer.
|
ToSByte
|
Overloaded. Converts a value to an 8-bit signed integer.
|
ToSingle
|
Overloaded. Converts a value to a single-precision floating-point number.
|
ToString
|
Overloaded. Converts a value to a string.
|
ToUInt16
|
Overloaded. Converts a value to a 16-bit unsigned integer.
|
ToUInt32
|
Overloaded. Converts a value to a 32-bit unsigned integer.
|
ToUInt64
|
Overloaded. Converts a value to a 64-bit unsigned integer.
|
Important points:
- The ToString and Parse methods are included in all of the data structure.
- In some situations where a string is expected, the compiler will automatically call the ToString method.
- The Convert class contains shared methods for converting all of the built-in-types.
Conclusion:
Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.