Widening and Narrowing Conversions
An important consideration with a type conversion is whether the result of the conversion is within the range of the destination data type. A widening conversion changes a value to a data type that can accommodate any possible value of the original data. A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values.
Widening Conversions
The following table shows the standard widening conversions.
Data type | Widens to data types 1 |
---|---|
SByte, Short, Integer, Long, Decimal, Single, Double |
|
Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double |
|
Short, Integer, Long, Decimal, Single, Double |
|
UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double |
|
Integer, Long, Decimal, Single, Double2 |
|
UInteger, Long, ULong, Decimal, Single, Double 2 |
|
Long, Decimal, Single, Double 2 |
|
ULong, Decimal, Single, Double 2 |
|
Decimal, Single, Double 2 |
|
Single, Double |
|
Double |
|
Any enumerated type (Enum) |
Its underlying integral type and any type to which the underlying type widens |
Char, String |
|
Char array |
Char array, String |
Any type |
|
Any derived type |
Any base type from which it is derived 3 |
Any type |
Any interface it implements |
Any data type or object type |
1 By definition, every data type widens to itself.
2 Conversions from Integer, UInteger, Long, ULong, or Decimal to Single or Double might result in loss of precision, but never in loss of magnitude. In this sense they do not incur information loss.
3 It might seem surprising that a conversion from a derived type to one of its base types is widening. The justification is that the derived type contains all the members of the base type, so it qualifies as an instance of the base type. In the opposite direction, the base type does not contain any new members defined by the derived type.
Widening conversions always succeed at run time and never incur data loss. You can always perform them implicitly, whether the Option Strict Statement sets the type checking switch to On or to Off.
Narrowing Conversions
The standard narrowing conversions include the following:
The reverse directions of the widening conversions in the preceding table (except that every type widens to itself)
Conversions in either direction between Boolean and any numeric type
Conversions from any numeric type to any enumerated type (Enum)
Conversions in either direction between String and any numeric type, Boolean, or Date
Conversions from a data type or object type to a type derived from it
Narrowing conversions do not always succeed at run time, and can fail or incur data loss. An error occurs if the destination data type cannot receive the value being converted. For example, a numeric conversion can result in an overflow. The compiler does not allow you to perform narrowing conversions implicitly unless the Option Strict Statement sets the type checking switch to Off.
When to Use Narrowing Conversions
You use a narrowing conversion when you know the source value can be converted to the destination data type without error or data loss. For example, if you have a String that you know contains either "True" or "False", you can use the CBool keyword to convert it to Boolean.
Exceptions During Conversion
Because widening conversions always succeed, they do not throw exceptions. Narrowing conversions, when they fail, most commonly throw the following exceptions:
InvalidCastException — if no conversion is defined between the two types
OverflowException — (integral types only) if the converted value is too large for the target type
If a class or structure defines a CType Function to serve as a conversion operator to or from that class or structure, that CType can throw any exception it deems appropriate. In addition, that CType might call Visual Basic functions or .NET Framework methods, which in turn could throw a variety of exceptions.
See Also
Tasks
How to: Convert an Object to Another Type in Visual Basic
Reference
Data Type Summary (Visual Basic)
Type Conversion Functions
Concepts
Data Types in Visual Basic
Implicit and Explicit Conversions
Value Changes During Conversions
Conversions Between Strings and Other Types
Array Conversions
Typeless Programming in Visual Basic