Data Types
An explicit conversion uses a type conversion keyword. Visual Basic provides several such keywords. These keywords work like functions, but the compiler generates the code inline, so execution is slightly faster than with a function call.
CType Function
The CType Function operates on two arguments. The first is the expression to be converted, and the second is the destination data type or object. Note that the first argument must be an expression, not a type. CType is an inline function, meaning the compiled code makes the conversion, often without generating a function call. This improves performance.
Code example
Dim objVal As Object Dim oRange As Excel.Range 'Perform conversion oRange = CType(objVal,Excel.Range)
List of inbuilt conversion functions
Conversion Keywords | Data type | Expression to be converted |
---|---|---|
CBool | Boolean | Any numeric type (including Byte, SByte, and enumerated types), String, Object |
CByte | Byte | Any numeric type (including SByte and enumerated types), Boolean, String, Object |
CChar | Char | String, Object |
CDate | Date | String, Object |
CDbl | Double | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CDec | Decimal | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CInt | Integer | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CLng | Long | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CObj | Object | Any type |
CSByte | SByte | Any numeric type (including Byte and enumerated types), Boolean, String, Object |
CShort | Short | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CSng | Single | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CStr | String | Any numeric type (including Byte, SByte, and enumerated types), Boolean, Char, Char array, Date, Object |
Type | Type specified following by comman (,) | When converting to an elementary data type (including an array of an elementary type), the same types as allowed for the corresponding conversion keyword When converting to a composite data type, the interfaces it implements and the classes from which it inherits When converting to a class or structure on which you have overloaded CType, that class or structure |
CUInt | UInteger | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CULng | ULong | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
CUShort | UShort | Any numeric type (including Byte, SByte, and enumerated types), Boolean, String, Object |
Note: Specifying CType to convert from one class type to another fails at run time if the source type does not derive from the destination type. Such a failure throws an InvalidCastException exception.
Please leave your comments or queries under comment section also please do subscribe to out blogs to keep your self upto date.