VB.NET Type Conversions

In this article I will explain you about casting and types of conversion in VB.NET.
  • 11905

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:

  1. Implicit Conversion (Widening Conversion)
  2. Explicit Conversion (Narrowing Conversion)

Implicit 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

Module
Module1
    Sub Main()
        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)
        Console.ReadLine()
    End Sub

End
Module

Explicit Conversion

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


Module
Module1
    Sub Main()
        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)
        Console.ReadLine()
    End Sub

End
Module

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.

 
Function    Description
CBool    to convert to Bool data type
CByte    to convert to Byte data type
CChar    to convert to Char data type
CDate    to convert to Date type
CDbl    to convert to Double data type
CDec    to convert to Decimal data type
CInt    to convert to Integer data type
CLng    to convert to Long data type
CObj    to convert to Object type
CShort    to convert to Short data type
CSng    to convert to Single data type
CStr    to convert to String data type
CUInt    to convert to UInteger data type
CULng    to convert to ULong data type
CUShort    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:


Module
Module1
    Sub Main()
        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)
        Console.ReadLine()
    End Sub

End
Module

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.

Conclusion:

Hope that this article might have helped you in understanding type conversion.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.