Implicit & explicit type Conversion in VB.net

By: Issac  

Implicit conversion is done automatically by .net compiler lets see a example of how it is done

Dim iDbl As Double
Dim iInt As Integer
iDbl = 1.13
MsgBox("The value of iDbl is " iDbl)
iInt = iDbl
MsgBox("The value of iInt is " iInt)

  • Declare a Double datatype variable iDble
  • Declare an Integer datatype variable iInt
  • Assign a decimal value to iDbl
  • Display the value of iDbl
  • Assign the value of iDbl to iInt
  • Display the value of iInt 

The first messagebox displays the value of iDbl as 1.13, the second messegebox display the value of iInt is 1, and iInt is displayed as 1 because the value is narrowed to 1 to fit in an Integer variable.Here the Compiler made the conversion for us. These types of conversions are called Implicit Conversion.
For this we need to keep the Option strict switch in OFF mode

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

Explicit Type Conversions 

While coding sometimes we may have to perform conversions which cannot be done by the compiler such conversions are called as Explicit conversions.An explicit conversion uses a type conversion keyword. With these conversion keywords we can perform the Explicit Conversion.




Archived Comments

1. Billyweimb
View Tutorial          By: Billyweimb at 2017-07-20 13:55:48


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial