< Day Day Up > |
As discussed earlier in this chapter, every piece of information in a Visual Basic .NET program has a particular type. When you are assigning a value to a variable or performing some operation on two values, all the types involved in the operation must be the same. For example, it is not possible to store a String value into an Integer variable, because the two types are not the same. When you are working with many different types of information, however, it is often necessary to convert one type of information to another. While it is not possible to store a String value into an Integer variable, it is possible to take a String value and convert it into an Integer value (which can then be stored into an Integer variable). By default, values in Visual Basic .NET will be converted from one type to another as needed. The compiler can do this because the types of all values and variables in Visual Basic .NET are always known and any needed conversions can be deduced from context. In the following example, the value in the String variable is implicitly converted to Integer when a is assigned to b . Dim a As String Dim b as Integer a = "10" b = a It is also possible to explicitly convert a value from one type to another by using the conversion operators, listed in Table 3-2.
Table 3-2. Conversion Operators
Each conversion operator takes a value of any type and attempts to convert it to the specified type. If the conversion cannot be accomplished, an exception will be thrown at runtime. The conversion operator CType is special in that the destination type is not part of the name of the operator; it is specified as part of the operation. Dim a As String Dim b as Integer a = "10" b = CInt(a) b = CType(a, Integer) Same as the previous statement The usefulness of the CType operator will become more apparent later on in the book, when we start dealing with inheritance and interfaces. There is also a special conversion operator, DirectCast , which is useful specifically in inheritance situations. It will also be covered in Chapter 13. Although the conversion operators are not required to convert from one type to another, using them is generally a good practice, especially when conversions may not succeed. The conversion operators call attention to the fact that a conversion is being performed. Sometimes explicit conversions may also be necessary because the type deduced is not correct. For example: Dim a, b As Byte Dim c as Short a = 255 b = 1 c = a + b ' Runtime error: overflow c = CShort(a) + CShort(b) ' Succeeds In the example, the expression a + b adds two Byte variables together. From this context, the result of the addition is assumed to be a Byte value ”the compiler does not know beforehand that the addition will result in 256 , which is not a valid Byte value. As a result, an error will occur even though the resulting type will fit into the ultimate destination (the variable c , which is typed as Short ). The expression CShort(a) + CShort(b) will succeed, however, because the addition of a Short to a Short is assumed to result in a Short value. Since 256 is a valid Short value, the operation will succeed. Widening and Narrowing ConversionsConversions between some types are always guaranteed to succeed, while others are not. In the following example, the first conversion may fail if the Short variable contains a value that will not fit into a Byte variable, whereas the second conversion will always succeed because any value that fits into a Byte variable will fit into a Short variable. Dim a As Byte Dim b As Short a = b b = a A conversion that is always guaranteed to succeed is called a widening conversion because the set of values that the destination type can hold is wider than the set of values that the source type can hold. In the preceding example, a conversion from Byte to Short is widening because every value of Byte is a valid value of Short as well. A conversion that might fail, however, is called a narrowing conversion because the set of values that the destination type can hold is narrower than the set of values that the source type can hold. Converting a Short value to Byte may or may not succeed, depending on whether the value is in the range of the Byte type. As discussed in the previous section, explicit conversions between types are not required. However, this can lead programmers into trouble ”it is easy when assigning from one variable to another to forget whether a conversion is being done and whether that conversion is widening or narrowing. If a widening conversion is taking place, there is nothing to worry about ”the conversion is guaranteed to succeed at runtime. But if a narrowing conversion is taking place, the programmer must think about what happens if that conversion should fail. Not handling a conversion failure could cause a serious bug in the application. Therefore, it is always a good idea to do narrowing conversions explicitly by using the appropriate conversion operator. To assist programmers in remembering to do so, the Option Strict statement can be placed at the beginning of a source file (or set as part of a project's options). One of the things that the Option Strict statement does is require narrowing conversions to be performed explicitly ”if a narrowing conversion is done without a conversion operator, the compiler will give an error.
Supported ConversionsVisual Basic .NET supports the following conversions between the fundamental types.
|
< Day Day Up > |