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.
- 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
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.