Udostępnij za pośrednictwem


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

SByte, Short, Integer, Long, Decimal, Single, Double

Byte

Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double

Short

Short, Integer, Long, Decimal, Single, Double

UShort

UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double

Integer

Integer, Long, Decimal, Single, Double2

UInteger

UInteger, Long, ULong, Decimal, Single, Double2

Long

Long, Decimal, Single, Double2

ULong

ULong, Decimal, Single, Double2

Decimal

Decimal, Single, Double2

Single

Single, Double

Double

Double

Any enumerated type (Enum)

Its underlying integral type and any type to which the underlying type widens

Char

Char, String

Char array

Char array, String

Any type

Object

Any derived type

Any base type from which it is derived 3

Any type

Any interface it implements

Nothing

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.

Note

The narrowing-conversion error is suppressed for conversions from the elements in a For Each…Next collection to the loop control variable. For more information and examples, see the "Narrowing Conversions" section in For Each...Next Statement (Visual Basic).

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:

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

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

Reference

Data Type Summary (Visual Basic)

Type Conversion Functions

Other Resources

Type Conversions in Visual Basic

Change History

Date

History

Reason

July 2008

Added a note about narrowing conversions and For Each…Next.

Customer feedback.