SiteMap  | About    

VB.NET Implicit and Explicit Conversions

Implicit Type Conversions

Implicit Conversion perform automatically in VB.NET, that is the compiler is taking care of the conversion.

The following example you can see how it happen.

1. Dim iDbl As Double

2. Dim iInt As Integer

3. iDbl = 9.123

4. MsgBox("The value of iDbl is " iDbl)

5. iInt = iDbl

6. MsgBox("The value of iInt is " iInt)

line no 1 : Declare a Double datatype variable iDble

line no 2 : Declare an Integer datatyoe variable iInt

line no 3 : Assign a decimal value to iDbl

line no 4 : Display the value of iDbl

line no 5 : Assign the value of iDbl to iInt

line no 6 : Display the value of iInt

The first messagebox display the value of iDbl is 9.123

The second messegebox display the value od iInt is 9

iInt display only 9 because the value is narrowed to 9 to fit in an Integer variable.

Here the Compiler made the conversion for us. These type fo conversions are called Implicit Conversion .

The Implicit Conversion perform only when the Option Strict switch is OFF

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim iDbl As Double
        Dim iInt As Integer
        iDbl = 9.123
        MsgBox("The value of iDbl is " & iDbl)
        iInt = iDbl
        'after conversion
        MsgBox("The value of iInt is " & iInt)
    End Sub
End Class

Explicit Type Conversions
In some cases we have to perform conversions , that is the compiler does not automatically convert a type to another . These type of conversion is called Explicit conversion . An explicit conversion uses a type conversion keyword. With these conversion keywords we hav to perform the Explicit Conversion. (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.