Type Conversion Keywords

As the compiler message states, Option Strict disallows implicit narrowing conversions. Option Strict does allow explicit narrowing conversions, however.

A narrowing conversion is performed explicitly using a type conversion keyword. Performing an explicit conversion also is known ascasting an expression to a given data type. This code uses the type conversion keyword CInt to convert, or cast, the value of MyReallyBigInt to an integer before assigning that value to the integer variable MyBigInt:

Dim MyReallyBigInt As Long = 1234567891

Dim MyBigInt As Integer = CInt(MyReallyBigInt) Table 5-2 lists the type conversion keywords.

Table 5-2: Type Conversion Keywords

Type Converts an Conversion Expression to Keyword Data Type

Allowable Data Types of Expression to be Converted

CBool Boolean

Any numeric type, String, Object

CByte Byte

Any numeric type, Boolean, String, Object

CChar Char CDate Date

String, Object String, Object

CDbl Double

Any numeric type, Boolean, String, Object

CDec Decimal

Any numeric type, Boolean, String, Object

CInt Integer

Any numeric type, Boolean, String, Object

CLng Long

Any numeric type, Boolean, String, Object

CObj Object

Any type

CShort Short

Any numeric type, Boolean, String, Object

CSng Single

Any numeric type, Boolean, String, Object

CStr String

Any numeric type, Boolean, String, Object

CType Type specified following the comma (,)

When converting to an elementary type, the same types as allowed for the corresponding conversion keyword

The syntax of using the type conversion keywords, with the exception of CType, is: [Property or variable] = [Type conversion keyword](value being converted)

Thus, in the preceding example, the type conversion keyword CInt converts (casts) the value stored in the Long variable MyReallyBigInt to an integer before assigning that value to the integer variable MyBigInt:

Dim MyBigInt As Integer = Clnt(MyReallyBiglnt)

The CType keyword uses a different syntax, with two arguments instead of one. The first argument is the expression to be converted, and the second argument is the destination data type or object class. The following example demonstrates the use of CType:

Dim MyBigInt As Integer = CType(MyReallyBigInt, Integer)

Using a type conversion keyword will not prevent overflow errors or rounding off. For example, the use of the CInt type conversion keyword does not prevent the overflow error discussed above with the same code absent the CInt type conversion keyword:

Dim MyReallyBigInt As Long = 2147483648

Dim MyBigInt As Integer = CInt(MyReallyBigInt)

Thus, the type conversion keyword does not alter the necessity that the source value (the value being assigned) must be valid for the destination data type. An error will occur if the value being assigned is not valid for the destination data type, such as here where the value being assigned is too large for the destination Integer data type, resulting in an overflow error.

Instead, the value of Option Strict is to ensure that you are aware that you are performing a narrowing conversion. In other words, it is Visual Basic .NET's way of asking you, as the programmer: "Are you really, really sure you want to do this?" If you do not use a type conversion keyword, the compiler will report to you an error such as: "Option Strict disallows implicit conversions from Long to Integer." If you do use a type conversion keyword, you necessarily must be aware that you are performing a narrowing conversion, for otherwise there would be no reason for you to use a type conversion keyword.

By contrast, without Option Strict, you could be performing narrowing conversions without knowing it. The result would be a runtime error, or perhaps even worse, a logic error in your code caused by, for example, rounding off.

While beginning programmers shy away from tools such as Option Strict that increase the number of build errors reported by the compiler, experienced programmers know that the compiler is the programmer's friend (perhaps only friend in the case of some antisocial programmers I know). You can resolve a build error quickly. By contrast, it might take you many hours to hunt down a logic error caused by an unwise narrowing conversion, assuming you even realize there is a problem at all.

Was this article helpful?

0 0

Post a comment