Udostępnij za pośrednictwem


Value Changes During Conversions

A conversion from a value type stores a copy of the source value in the destination of the conversion. However, this copy is not an exact image of the source value. The destination data type stores values differently, and even the value being represented might change, depending on the kind of conversion being performed.

Changes During Widening and Narrowing Conversions

Narrowing conversions change the destination copy of the source value, with potential information loss. For example, a fractional value is rounded when converted to an integral type, and a numeric type being converted to Boolean is reduced to either True or False.

Widening conversions preserve the source value but can change its representation. This happens if you convert from an integral type to Decimal, or from Char to String.

The original source value is not changed as a result of a conversion.

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

Tasks

How to: Convert an Object to Another Type in Visual Basic

Concepts

Widening and Narrowing Conversions

Implicit and Explicit Conversions

Conversions Between Strings and Other Types

Array Conversions

Typeless Programming in Visual Basic

Reference

Data Type Summary (Visual Basic)

Type Conversion Functions

Other Resources

Type Conversions in Visual Basic

Structures: Your Own Data Types