擴展和縮小轉換 (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, 或StringChar轉換為, 就會發生此情況。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的數數值型別縮小True為或False時, 就會將其四捨五入。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 擴大至資料類型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 Short, Integer, Long, Decimal, Single, DoubleShort, Integer, Long, Decimal, Single, Double
UShortUShort UShort, Integer, UInteger, Long, ULong, Decimal, Single, DoubleUShort, Integer, UInteger, Long, ULong, Decimal, Single, Double
IntegerInteger Integer, Long, Decimal, Single, Double2Integer, Long, Decimal, Single, Double2
UIntegerUInteger UInteger, Long, ULong, Decimal, Single, Double2UInteger, Long, ULong, Decimal, Single, Double2
LongLong Long, Decimal, Single, Double2Long, Decimal, Single, Double2
ULongULong ULong, Decimal, Single, Double2ULong, Decimal, Single, Double2
DecimalDecimal Decimal, Single, Double2Decimal, Single, Double2
SingleSingle SingleDoubleSingle, Double
DoubleDouble Double
任何列舉型別 (Enum)Any 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.

2 Integer從、 、、Single或轉換為或Double可能會導致精確度遺失, 但不會遺失大小。 Decimal Long UInteger ULong2 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

  • 從任何數數值型別轉換成任何列舉類型 (Enum)Conversions 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 語句將類型檢查參數設定為Off, 否則編譯器不允許您隱含執行縮小轉換。The 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