In many situations, you will need to convert variables from one type into another. Table 2.4 shows the methods of the Convert class that perform data-type conversions.
In addition to the methods of the Convert class, you can still use the data-conversion functions of VB (CInt() to convert a numeric value to an Integer, CDbl() to convert a numeric value to a Double, CSng() to convert a numeric value to a Single, and so on), which you can look up in the documentation. If you’re writing new applications in VB 2008, use the new Convert class to convert between data types.
To convert the variable initialized as the following
Dim A As Integer
to a Double, use the ToDouble method of the Convert class:
Dim B As Double
B = Convert.ToDouble(A)
Suppose that you have declared two integers, as follows:
Dim A As Integer, B As Integer
A = 23
B = 7
The result of the operation A / B will be a Double value. The following statement
Debug.Write(A / B)
displays the value 3.28571428571429. The result is a Double value, which provides the greatest possible accuracy. If you attempt to assign the result to a variable that hasn’t been declared as Double, and the Strict option is on, then VB 2008 will generate an error message. No other data type can accept this value without loss of accuracy. To store the result to a Single variable, you must convert it explicitly with a statement like the following:
Convert.ToSingle(A / B)
You can also use the DirectCast() function to convert a variable or expression from one type to another. The DirectCast() function is identical to the CType() function. Let’s say the variable A has been declared as String and holds the value 34.56. The following statement converts the value of the A variable to a Decimal value and uses it in a calculation:
Dim A As String = "34.56"
Dim B As Double
B = DirectCast(A, Double) / 1.14
The conversion is necessary only if the Strict option is on, but it’s a good practice to perform your conversions explicitly. The following section explains what might happen if your code relies on implicit conversions.
Table 2.4 – The Data-Type Conversion Methods of the Convert Class
|Method||Converts Its Argument To|
|ToInt16||Short Integer (2-byte integer, Int16)|
|ToInt32||Integer (4-byte integer, Int32)|
|ToInt64||Long (8-byte integer, Int64)|
|CShort||Short (2-byte integer, Int16)|
|ToUInt16||Unsigned Integer (2-byte integer, Int16)|
|ToUInt32||Unsigned Integer (4-byte integer, Int32)|
|ToUInt64||Unsigned Long (8-byte integer, Int64)|
Widening and Narrowing Conversions
In some situations, VB 2008 will convert data types automatically, but not always. Let’s say you have declared and initialized two variables, an Integer and a Double, with the following statements:
Dim count As Integer = 99
Dim pi As Double = 3.1415926535897931
If the Strict option is off and you assign the variable pi to the count variable, the count variable’s new value will be 3. (The Double value was rounded to an Integer value, according to the variable’s type.) Although this may be what you want, in most cases it’s an oversight that will lead to incorrect results.
If the Strict option is on and you attempt to perform the same assignment, the compiler will generate an error message to the effect that you can’t convert a Double to an Integer. The exact message is Option Strict disallows implicit conversions from Double to Integer.
When the Strict option is on, VB 2008 will perform conversions that do not result in loss of accuracy (precision) or magnitude. These conversions are called widening conversions. When you assign an Integer value to a Double variable, no accuracy or magnitude is lost. This is a widening conversion, because it goes from a narrower to a wider type.
On the other hand, when you assign a Double value to an Integer variable, some accuracy is lost (the decimal digits must be truncated). This is a narrowing conversion, because we go from a data type that can represent a wider range of values to a data type that can represent a narrower range of values.
Because you, the programmer, are in control, you might want to give up the accuracy — presumably, it’s no longer needed. Table 2.5 summarizes the widening conversions that VB 2008 will perform for you automatically.
Table 2.5: Visual Basic 2008 Widening Conversions
|Original Data Type||Wider Data Type|
|Byte||Short, Integer, Long, Decimal, Single, Double|
|Short||Integer, Long, Decimal, Single, Double|
|Integer||Long, Decimal, Single, Double|
|Long||Decimal, Single, Double|
If the Strict option is on, the compiler will point out all the statements that may cause runtime errors, and you can reevaluate your choice of variable types. You can also turn on the Strict option temporarily to see the compiler’s warnings, and then turn it off again.