擴展和縮小轉換 (Visual Basic)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. 如果您從整數類資料類型轉換成 Decimal,或從 Char 轉換成 String,就會發生這種情況。This occurs if you convert from an integral type to Decimal, or from Char to String.

「縮小轉換」 (narrowing conversion) 會將值變更為資料類型,而可能無法保留一些可能的值。A narrowing conversion changes a value to a data type that might not be able to hold some of the possible values. 例如,當小數值轉換成整數類資料類型時,會將其四捨五入,而轉換成 Boolean 的數數值型別則會縮減為 TrueFalseFor 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 擴大至資料類型1Widens to data types 1
SByteSByte SByte, Short, Integer, Long, Decimal, Single, DoubleSByte, Short, Integer, Long, Decimal, Single, Double
ByteByte Byte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, DoubleByte, Short, UShort, Integer, UInteger, Long, ULong, Decimal, Single, Double
ShortShort ShortIntegerLongDecimalSingleDoubleShort, Integer, Long, Decimal, Single, Double
UShortUShort UShort, Integer, UInteger, Long, ULong, Decimal, Single, DoubleUShort, Integer, UInteger, Long, ULong, Decimal, Single, Double
IntegerInteger IntegerLongDecimalSingleDouble2Integer, Long, Decimal, Single, Double2
UIntegerUInteger UIntegerLongULongDecimalSingleDouble2UInteger, Long, ULong, Decimal, Single, Double2
LongLong LongDecimalSingleDouble2Long, Decimal, Single, Double2
ULongULong ULongDecimalSingleDouble2ULong, Decimal, Single, Double2
DecimalDecimal DecimalSingleDouble2Decimal, Single, Double2
SingleSingle SingleDoubleSingle, Double
DoubleDouble Double
任何列舉型別(EnumAny enumerated type (Enum) 其基礎整數類型,以及基礎類型所擴展的任何類型。Its underlying integral type and any type to which the underlying type widens.
CharChar CharStringChar, String
Char 陣列Char array Char 陣列,StringChar array, String
任何類型Any type 物件Object
任何衍生類型Any derived type 衍生它的任何基底類型3Any base type from which it is derived 3.
任何類型Any type 它所實行的任何介面。Any interface it implements.
NothingNothing 任何資料類型或物件類型。Any data type or object type.

1根據定義,每個資料類型會擴展到其本身。1 By definition, every data type widens to itself.

2IntegerUIntegerLongULongDecimal 轉換成 SingleDouble 可能會導致精確度遺失,但不會遺失大小。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在從衍生型別轉換成其中一個基底類型時,它看起來可能會很令人驚訝。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. 無論Option Strict 語句是否將類型檢查參數設定為 OnOff,一律可以隱含地執行它們。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

  • 從任何數數值型別轉換成任何列舉類型(EnumConversions from any numeric type to any enumerated type (Enum)

  • 字串與任何數數值型別、Boolean日期之間任一方向的轉換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. 編譯器不允許您隱含執行縮小轉換,除非Option Strict 語句將類型檢查參數設定為 OffThe compiler does not allow you to perform narrowing conversions implicitly unless the Option Strict Statement sets the type checking switch to Off.

注意

For Each…Next 集合中的元素轉換為迴圈控制變數時,會抑制縮小轉換錯誤。The narrowing-conversion error is suppressed for conversions from the elements in a For Each…Next collection to the loop control variable. 如需詳細資訊和範例,請參閱 For Each ... 中的「縮小轉換」一節。 下一個語句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. 例如,如果您的 String 知道包含 "True" 或 "False",則可以使用 CBool 關鍵字將它轉換成 BooleanFor 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:

如果類別或結構定義了一個CType函式,以做為該類別或結構的轉換運算子,CType 可能會擲回任何符合的例外狀況。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. 此外,CType 可能會呼叫 Visual Basic 函數或 .NET Framework 方法,而這可能會擲回各種例外狀況。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