VB.NET Implicit and Explicit Conversions

Implicit Type Conversions

In VB.NET, implicit conversion is a feature that allows the compiler to automatically perform data type conversions when it is safe and logical to do so. The purpose of implicit conversion is to simplify programming by handling data type conversions behind the scenes without the need for explicit coding.

Implicit conversion occurs when the target data type can fully accommodate the values of the source data type without any risk of data loss or truncation. For example, converting an Integer to a Long, or a Single to a Double, can be done implicitly because the target data type can hold a wider range of values than the source data type.

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)
  1. line no 1 : Declare a Double datatype variable iDble
  2. line no 2 : Declare an Integer datatyoe variable iInt
  3. line no 3 : Assign a decimal value to iDbl
  4. line no 4 : Display the value of iDbl
  5. line no 5 : Assign the value of iDbl to iInt
  6. 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

Full Source VB.NET
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

Explicit conversion is used when the compiler cannot automatically convert one data type to another, and the programmer needs to explicitly specify the conversion using type conversion keywords. Explicit conversion is necessary when the conversion may result in a loss of data or when converting between incompatible data types.

To perform explicit conversion, you use the type conversion keywords provided by VB.NET. Some of the commonly used type conversion keywords are:

CType

The CType keyword is used to explicitly convert a value from one data type to another compatible data type. It performs a runtime conversion and can handle both widening and narrowing conversions.

Dim numDouble As Double = 3.14 Dim numInt As Integer = CType(numDouble, Integer) ' Explicit conversion from Double to Integer

DirectCast

The DirectCast keyword is used for reference type conversions, where you explicitly convert an object from one type to another, but only if the conversion is valid for the specified types.

Dim obj As Object = "Hello" Dim str As String = DirectCast(obj, String) ' Explicit conversion from Object to String

CInt, CDbl, CStr, etc.

These are specific type conversion functions that allow explicit conversions between specific data types. For example, CInt is used to convert a value to an Integer, CDbl to convert to a Double, and CStr to convert to a String.

Dim numStr As String = "123" Dim numInt As Integer = CInt(numStr) ' Explicit conversion from String to Integer

Conclusion

It's important to note that explicit conversion should be used with caution, as it may result in data loss or unexpected behavior if the conversion is not valid or supported. It is the programmer's responsibility to ensure that the conversion is appropriate and does not lead to any undesired outcomes.