+92 332 4229 857 99ProjectIdeas@Gmail.com

Conversions - Implicit and Explicit (Vb.net)


Conversions

Types of conversions

·         Implicit conversion
·         Explicit conversion

Implicit conversion

Implicit conversion is a type of conversion which is automatically performed by the compiler, you do not have to mention. Implicit conversions are only allowed when Option Strict is set to be Off. If Option Strict is On, then the compiler will not allow implicit conversions.

Example

Dim intVar As Integer = 0
Dim doubleVar As Double = 12.345
intVar = doubleVar
MsgBox(intVar)

Explicit conversion

Explicit conversion is a type of conversion, which is not performed automatically by the compiler. Programmer should have to mention through the use of built-in conversion functions. Most commonly explicit conversions are done through CType function. Other function used for explicit conversions are CStr, CInt, CDbl, CDate, CDec, CByte etc.

Example


Dim intVar As Integer = CType(55.7, Integer)
MsgBox(intVar)
Dim doubleVar As Double = 12.345
Dim intVar As Integer = 0
intVar = CInt(doubleVar)
MsgBox(intVar)

Dim doubleVar As Double = 12.345
Dim strVar As String = String.Empty
strVar = CStr(doubleVar)
MsgBox(strVar)

0 comments: