Export (0) Print
Expand All

Implicit and Explicit Conversions

An implicit conversion does not require any special syntax in the source code. In the following example, Visual Basic .NET implicitly converts the value of K to single-precision floating point before assigning it to Q.

Dim K As Integer
Dim Q As Double
' ...
K = 432   ' Integer widens to Double, so Option Strict can be On.
Q = K

An explicit conversion uses a type conversion keyword. Visual Basic .NET provides several such keywords, which coerce an expression in parentheses to the desired data type. These keywords act like functions, but the compiler generates the code inline, so execution is slightly faster than with a function call.

In the following extension of the preceding example, the CInt keyword converts the value of Q back to an integer before it is assigned to K:

Q = Math.Sqrt(Q)   ' Q had been assigned the value 432 from K.
K = CInt(Q)   ' K now has the value 21 (rounded square root of 432).

The following table shows the available conversion keywords.

Type conversion keyword Converts an expression to data type Allowable data types of expression to be converted
CBool Boolean Any numeric type (including Byte and enumerated types), String, Object
CByte Byte Any numeric type, any enumerated type, Boolean, String, Object
CChar Char String, Object
CDate Date String, Object
CDbl Double Any numeric type (including Byte and enumerated types), Boolean, String, Object
CDec Decimal Any numeric type (including Byte and enumerated types), Boolean, String, Object
CInt Integer Any numeric type (including Byte and enumerated types), Boolean, String, Object
CLng Long Any numeric type (including Byte and enumerated types), Boolean, String, Object
CObj Object Any type
CShort Short Any numeric type (including Byte and enumerated types), Boolean, String, Object
CSng Single Any numeric type (including Byte and enumerated types), Boolean, String, Object
CStr String Any numeric type (including Byte), Boolean, Char, Char() array, Date, Object
CType Type specified following the comma (,) When converting to an elementary type (including an array of an elementary type), the same types as allowed for the corresponding conversion keyword

When converting to a composite type, the interfaces it implements and the classes from which it inherits

The CType Keyword

The CType keyword operates on two arguments. The first is the expression to be converted, and the second is the destination data type or object class. The following example demonstrates the use of CType.

K = CType(Q, Integer)  ' Uses CType keyword instead of CInt.
F = CType(W, Label)    ' Coerces W to the specific object class Label.

You can use CType to convert values to composite data types as well as to elementary types. You can also use it to coerce an object class to the type of one of its interfaces, as in the following example:

' Assume class CZone implements interface IZone.
Dim H As Object
Dim CZ As CZone   ' Cannot use a type as first argument of CType.
H = CType(CZ, IZone)   ' Coerces CZone object to its interface IZone.

CType can also convert array types, as in the following example:

Dim V() As ClassV   ' Array of ClassV objects.
Dim ObArray() As Object   ' Array of objects to be assigned.
' Some object array is assigned to ObArray.
If TypeOf ObArray Is ClassV()   ' Check for run-time type compatibility.
   V = CType(ObArray, ClassV())   ' ObArray can be converted to ClassV.
End If
Note   Values used with a conversion keyword must be valid for the destination data type, or an error occurs. For example, if you attempt to convert a Long to an Integer, the value of the Long must be within the valid range for the Integer data type.
Caution   Specifying CType to convert from one class type to another fails at run time if there is no inheritance relationship between the two types.

Performing an explicit conversion is also known as casting an expression to a given data type or object class.

See Also

Widening and Narrowing Conversions | Value Changes During Conversions | Conversions Between Strings and Other Types | Structures: Your Own Data Types | Typeless Programming | Data Type Summary | Type Conversion Functions

Show:
© 2014 Microsoft