Widening Conversions

The value 1234567891 can be stored in either an Integer or Long data type. The following code assigns that value to both an Integer and a Long variable, using a type conversion:

Dim MyBigInt As Integer = 1234567891 Dim MyReallyBigInt As Long = MyBigInt

The first line of code assigns the value 1234567891 to a 32-bit Integer variable MyBigInt. This presents no problem, since 1234567891 is within the range of an Integer variable. However, the second line of code attempts to assign the value that is stored in the 32-bit Integer variable MyBigInt to a 64-bit Long variable MyReallyBigInt. This does present a problem, as a 64 -bit value, not a 32-bit value, should be assigned to a 64-bit variable. Therefore, to accomplish this assignment, Visual Basic .NET first converts the 32-bit value stored in MyBigInt to a 64-bit value before it is assigned to the 64-bit variable MyReallyBigInt. MyBigInt remains a 32-bit Integer variable; only the value assigned to MyReallyBigInt is converted from 32 bits to 64 bits.

This is a widening conversion since it increases the number of bits, here from 32 to 64 bits. A widening conversion is considered safe in terms of the risk of losing data. For example, any value stored in a 32-bit Integer variable also can be stored in a 64-bit Long variable.

Table 5-1 shows the standard widening conversions.

Table 5-1: Widening Conversions

Data Type

Widens to Data Types

Byte

Short, Integer, Long, Decimal, Single, Double

Short

Integer, Long, Decimal, Single, Double

Integer

Long, Decimal, Single, Double

Long

Decimal, Single, Double

Decimal

Single, Double

Single

Double

Char

String

Any type

Object

Was this article helpful?

0 0

Post a comment