Conversões de expansão e restrição (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 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, Double 2

Long

Long, Decimal, Single, Double 2

ULong

ULong, Decimal, Single, Double 2

Decimal

Decimal, Single, Double 2

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

Charmatriz

Chararray,String

Any type

Objeto

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 Opção declaração estrito 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 Opção declaração estrito sets the type checking switch to Off.

ObservaçãoObservação

O erro de conversão-restrição é suprimida para conversões de elementos em um For Each…Next variávelde controle decoleção para o loop . Para obter mais informações e exemplos, consulte "Restringir conversões" seção Instrução For Each...Next (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 Função CType (Visual Basic) 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.

Consulte também

Tarefas

Como: Converter um objeto para outro tipo no Visual Basic

Referência

Resumo de tipo de dados (Visual Basic)

Funções de conversão de tipo (Visual Basic)

Conceitos

Tipos de dados no Visual Basic

Conversões explícitas e implícitas (Visual Basic)

Alterações de valor durante conversões (Visual Basic)

Conversões entre sequências e outros tipos (Visual Basic)

Conversões de matriz (Visual Basic)

Programação Sem-Tipos no Visual Basic

Outros recursos

Conversões de Tipo no Visual Basic