Uredi

Deli z drugimi prek


Widening and Narrowing Conversions (Visual Basic)

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 allow for any possible value of the original data. Widening conversions preserve the source value but can change its representation. This occurs if you convert from an integral type to Decimal, or from Char to String.

A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. For example, a fractional value is rounded when it is converted to an integral type, and a numeric type being converted to Boolean is reduced to either True or False.

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.

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.

Changes During Reference Type Conversions

A conversion from a reference type copies only the pointer to the value. The value itself is neither copied nor changed in any way. The only thing that can change is the data type of the variable holding the pointer. In the following example, the data type is converted from the derived class to its base class, but the object that both variables now point to is unchanged.

' Assume class cSquare inherits from class cShape.  
Dim shape As cShape  
Dim square As cSquare = New cSquare  
' The following statement performs a widening  
' conversion from a derived class to its base class.  
shape = square  

See also