Learning Visual Basic s Type Rules


Learning Visual Basic's Type Rules

Recall that Visual Basic is a strongly typed language. This implies that all variables declared in a Visual Basic program must be given an explicit data type. Furthermore, the set of values that can be assigned to a variable are limited by the variable's type. That is, a variable that is of type Integer can be assigned only positive or negative whole number values that range between, approximately, positive two billion and negative two billion.

What happens, though, when you try to assign a nonintegral number to an integer variable, or when you try to assign an integer to a nonintegral numeric variable? What about when you try to assign a string variable to a nonintegral numeric variable, or an integer variable to a string variable?

Because Visual Basic is strongly typed, a value of one type cannot be assigned to a variable of a different type. That means you should not be able to assign an integer value to a nonintegral numeric variable. However, the following code will work fine:

Dim fpVariable as Single fpVariable = 5 


Why does the preceding code snippet not produce an error? After all, doesn't it violate the typing rules of Visual Basic by assigning an integer value to a nonintegral numeric variable?

Such an assignment is legal because, behind the scenes, Visual Basic casts the integer value 5 into a nonintegral value (5.0) and then assigns it to the nonintegral numeric variable.

Understanding Casting

Casting is the process of changing the type of a variable or value from one type to another. There are two types of casting: implicit casting and explicit casting.

Implicit casting occurs when the casting occurs without any needed intervention by the programmer. In the code snippet we looked at last, implicit casting is utilized because the integer variable 5 is cast to a nonintegral representation of 5.0 without any extra code provided by us, the programmers.

By the Way

The documentation accompanying the .NET Framework SDK refers to implicit casting as coercion.


Explicit casting, on the other hand, requires that we, the programmers, explicitly indicate that a cast from one type to another should occur. To explicitly cast a variable from one type to another, we use Visual Basic's built-in CType() function, which has the following syntax:

CType(variableName, typeToCastTo) 


The CType() function casts the variable variableName from its current type to the type specified by typeToCastTo. For example, imagine the code snippet we looked at earlier implicitly casting an integer to a Single. We can make this cast explicit by using the following code:

Dim fpVariable as Single fpVariable = CType(5, Single) 


Here, the CType() function is used to explicitly cast the integer value 5 to a Single, which is then assigned to the Single variable fpVariable.

Did you Know?

In addition to Visual Basic's CType function, there is a Convert class in the .NET Framework that contains methods of the form ToDataType(). For example, to explicitly cast the integer 5 to a Single type, you could use

fpVariable = Convert.ToSingle(5) 



Widening and Narrowing Casts

Visual Basic can be run in one of two modes: strict and nonstrict. In the strict mode, implicit casting is allowed only for widening casts. In a widening cast, the set of legal values for one data type is a subset of the set of legal values for the data type the variable is being cast to. For example, a cast from an integer to a nonintegral number is a widening cast because every possible integer value can be expressed as a nonintegral value.

Another example of a widening cast is casting a variable of type Integer to a variable of type Long. This is a widening cast because any legal value for an Integer is included in the legal values for a Long.

The opposite of a widening cast is a narrowing cast. Consider casting a nonintegral numeric variable to an integer. If the nonintegral numeric variable has the value 5.0, we can safely cast this to the integer 5. But what if the variable has the value 3.14? There is no integer value that can represent 3.14 precisely. When we're casting 3.14 to an integer, the resulting integer value is 3; the remainder is dropped.

Watch Out!

In a narrowing cast there is the potential for lost information. When we're casting 3.14 to an integer, the 0.14 portion of the number is lost in the narrowing cast.


Rules for Implicit Casting

Considering that narrowing casts can result in a loss of data, should Visual Basic allow for implicit narrow casts, or would it be prudent for the language to require that an explicit cast be used if a narrowing cast is required?

Truly strongly typed programming languages would require that all narrowing casts be explicit. In this case, if you tried to use code that would invoke an implicit narrowing cast, an error would result. That is, in a truly strongly typed programming language, the following code would produce an error:

Dim x as Integer x = 8 / 4 


The reason this would result in an error is that the / operator returns a nonintegral number (2.0), which must be cast to an integer in order to assign it to x. However, this cast is a narrowing cast. In a truly strongly typed language, you would have to provide an implicit cast, like so:

Dim x as Integer x = CType(8 / 4, Integer) 


or

Dim x as Integer x = Convert.ToInteger(8 / 4) 


The disadvantage of requiring explicit casting for narrowing casting is that older versions of Visual Basic allowed for implicit narrowing casts, meaning that old Visual Basic code could not be reused as-is in programs created with today's version of Visual Basic; rather, the programmer would have to alter the code to include explicit casting.

The designers of Visual Basic decided to take a middle-of-the-road approach. By default, implicit narrowing casts are permitted, meaning that the code

Dim x as Integer x = 8 / 4 


will run without error, assigning the integer value 2 to the variable x.

Watch Out!

Keep in mind that casting a nonintegral number to an integer drops the remainder portion of the nonintegral number. That is, after the following code is executed, the value of x will be 0:

Dim x as Integer x = 3 / 4 



To disallow implicit narrow casting on an ASP.NET page, open it by double-clicking on the file in the Solution Explorer. Next, in the Properties window go to the properties for the DOCUMENT (select the DOCUMENT item from the Properties window's drop-down list). In the ASP.NET section, you'll find an option titled Strict. Set this to True to disallow implicit narrow casting. Figure 5.2 shows the Properties window and the Strict option set to True.

Figure 5.2. The Strict option can be found in the ASP.NET section of the Properties window for the DOCUMENT.


When you set the Strict option to True, Visual Basic will not allow implicit narrowing casts. You will get a compile-time error message if you try to perform an implicit narrowing cast. For example, say you create an ASP.NET web page whose source code content contains the following code in the Page_Load event handler:

Dim a as Integer a = 8/4    'An implicit narrowing cast will occur here! 


In this case, you will get the error shown in Figure 5.3 when viewing the ASP.NET page through a browser.

Figure 5.3. A compile-time error occurs if Strict="True" is set and an implicit narrowing cast is found.


By the Way

For this book, I will not be adding Strict="True". In some code examples there may be implicit narrow casting. If you choose to add Strict="True", you will have to add the appropriate explicit casting for such code examples.





Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net