|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Widening and Narrowing Conversions
An important characteristic of 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.
The following table shows the standard widening conversions.
|Data type||Widens to data types|
|Byte||Byte, Short, Integer, Long, Decimal, Single, Double|
|Short||Short, Integer, Long, Decimal, Single, Double|
|Integer||Integer, Long, Decimal, Single, Double|
|Long||Long, Decimal, Single, Double|
|Decimal||Decimal, Single, Double|
|Any enumerated type||Its underlying integer type and any type to which that widens|
|Any derived type||Any base type from which it is derived|
|Any type||Any interface it implements|
|Nothing||Any data type or object type|
Conversions from Integer to Single, from Long to Single or Double, or from 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.
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 have the members defined by the derived type.
Widening conversions always succeed and can always be performed implicitly.
The standard narrowing conversions include:
- The reverse directions of the widening conversions in the preceding table
- Conversions in either direction between Boolean and any numeric type
- Any numeric type to any enumerated type
- Conversions in either direction between a Char() array and a String
- 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, meaning that they might fail at run time. 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 usually does not allow narrowing conversions to be performed implicitly.
You use a narrowing conversion when you know the source value can be converted to the destination data type without error. 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.
Implicit and Explicit Conversions | Value Changes During Conversions | Conversions Between Strings and Other Types | Typeless Programming | Data Type Summary | Type Conversion Functions | Boolean Data Type | Byte Data Type | Char Data Type | Date Data Type | Decimal Data Type | Double Data Type | Integer Data Type | Long Data Type | Object Data Type | Short Data Type | Single Data Type | String Data Type