擴展和縮小轉換 (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擴展轉換值變更為可允許任何可能的值,原始資料的資料類型。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,或從CharStringThis 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 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從不IntegerUIntegerLongULong,或DecimalSingleDouble可能會導致遺失有效位數,但永遠不會遺失大小。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 陳述式設定檢查參數的型別OnOffYou 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 陳述式設定類型檢查參數以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 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