Type Conversions in VB.NET
In this article I will explain you about casting and types of conversion in VB.NET.
Definition: Casting refers to the process of converting an expression from one data type to another.
Conversion is based on type compatibility and data compatibility.
There are two types of conversions:
- Implicit Conversion (Widening Conversion)
- Explicit Conversion (Narrowing Conversion)
A Widening conversion is one that casts data from a data type with a narrower range of possible values to a data type with a wider range of possible values.
In implicit conversion the compiler will make conversion for us without asking.
Byte -> Short -> Integer -> Long -> Decimal -> Single -> Double
is an example of data compatibility.
Complier checks for type compatibility at compilation.
- Visual Basic uses implicit cast to do widening conversion automatically.
- When you do arithmetic expression, Visual Basic does widening conversions implicitly so all operands have the widest data type used in the expression.
Practical demonstration of implicit conversion
Dim num1 As Integer = 100
Dim num2 As Integer = 75
Dim total As Long
'In this the Integer values are implicitly converted to Long data type
'you need not to tell compiler to do the conversion, it automatically does
total = num1 + num2
Console.WriteLine("Total is : " & total)
A Narrowing conversion is one that casts data from a wider data type to a narrower data type.
In explicit conversion we specifically ask the compiler to convert the value into another data type.
CLR checks for data compatibility at runtime.
Explicit conversion is carried out using casts. When we cast one type to another, we deliberately force the compiler to make the transformation.
You should never expect that the cast would give you best or correct result. Casts are potentially unsafe. Casting of big data type into small may lead to loosing of data.
Practical demonstration of explicit conversion
Dim num As Integer
Dim marks As Decimal = 34.75
'In this the Decimal values are explicitly converted to Integer data type with rounding the marks 35
'you have to tell compiler to do the conversion, it uses casting
num = CInt(marks)
Console.WriteLine("Converted value is: " & num)
In arithmetic expression, explicit casts are done before any of the other operations.
Below is the list of conversion functions, which we can use in VB .NET.
|| to convert to Bool data type
|| to convert to Byte data type
|| to convert to Char data type
|| to convert to Date type
|| to convert to Double data type
|| to convert to Decimal data type
|| to convert to Integer data type
|| to convert to Long data type
|| to convert to Object type
|| to convert to Short data type
|| to convert to Single data type
|| to convert to String data type
|| to convert to UInteger data type
|| to convert to ULong data type
|| to convert to UShort data type
CType function for conversion
If we are not sure of the name of a particular conversion function then we can use the CType function. See the program below:
Dim text As String = "25.56"
Dim per As Double
'two arguments, type we are converting from, to type desired
per = CType(text, Double) + 1.14
Console.WriteLine("Integer value is: " & per)
In the above program CType() function is used to convert a variable or expression from one type to another. The variable text has been declared as String and holds the value "25.56" The following statement converts the value of the text variable to a Decimal value and uses it in a calculation.
Hope that this article might have helped you in understanding type conversion.
Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.