Option Statements

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 2.  Out with the Old, In with the New


Visual Basic 6 supported several Option statements:

  • Option Explicit required the programmer to declare all variables .

  • Option Base [01] redesignated the lower bound index of arrays.

  • Option Compare [BinaryTextDatabase] indicated the default behavior of string comparisons.

  • Option Private indicated that module code was private to the hosting application.

A few of these option statements made it to Visual Basic .NET, but Option Base didn't.

Option Explicit

Option Explicit was carried over into Visual Basic .NET. You can set the project level Option Explicit mode to On or Off for the project in the Build view of the Property Pages and override it locally in the module.

The default value at the project level is Option Explicit On. This is true for a reason. It's bad to have ambiguous variable declarations.

Scope boundaries are defined by blocks of code such as procedures, modules, classes, and applications. It's possible in the same application to have two variables or procedures with the same name. Without scope rules, we would run into name conflicts.

Option Explicit On helps you avoid the case where you implicitly declare a variable, or so you think, but a variable with an identical name exists in a broader scope. Your code ends up modifying that variable unintentionally.

A second problem existed in VB6: implicit variables were variants. Variant data types aren't very efficient, and were intended to support COM rather than as a general programming tool.

Explicit variable declaration means that the variable is declared using the Dim statement and expressing a data type. Implicit variable declaration is when you introduce a variable without a Dim statement. (Refer to the section "Variable Declarations" for more details.) When Option Explicit On is set, you have to declare all variables with the Dim keyword before you use them. By modifying Module1 from Listing 2.1, we can demonstrate in Listing 2.2 the behavior of Option Explicit in either mode.

Listing 2.2 Demonstrating Option Explicit
  1:  Option Explicit Off  2:   3:  Module Module1  4:   5:  Sub Test()  6:  Message = "Module1"  7:  MsgBox(Message)  8:  End Sub  9:   10:  End Module 

Tip

You want as much of your code as possible to exhibit any errors it contains at compile time. Option Explicit On supports this notion.


The code in Listing 2.2 works as you might expect. The Message variable is declared implicitly and initialized to the string "Module1." If line 1 is changed to Option Explicit On, the code in Listing 2.2 will cause a compiler error. To declare the Message variable as a string and initialize it, redefine line 6 as follows :

 Dim Message As String = "Module1" 

The statement now explicitly declares Message as a string and initializes the string. (Single statement declaration and initialization is new in Visual Basic .NET. See the section on "Variable Declarations" for details.)

Visual Basic .NET doesn't support the Variant data type. Instead you get an Object type with implicit declarations. This is never what you want. You always want to declare a specific type, allowing the compiler to help you as much as possible. If you actually need a generic type, declare a variable of type Object. (See the section "Data Types" for more on Object types.)

Note

Much of the available literature indicates that implicitly declared variables will be Object types. In Visual Basic .NET everything is an object; however, in actuality, it does appear that the compiler is performing some type conversion based on the value assigned to the implicitly declared variable. Revise Listing 2.2, line 7 to MsgBox(Message.GetType().Name) with Option Explicit Off, and the application will report that Message is a String.

If you initialized Message to an integer value, for example Message = 1, and requested type information with Option Explicit Off, the reported type would be an Int32 type. GetType reports type information based on the contents of the variable at the time GetType is called.


Option Compare

The default comparison is Option Compare Binary. A Binary comparison is also referred to as a case-sensitive comparison. The alternate choice is Option Compare Text. This statement is set to Binary at the project level; it can be changed at the project level and overridden at the module level.

Option Compare Database is no longer supported in VB .NET.

Option Strict

Option Strict is a new compiler directive. Option Strict Off is the default mode. The preferred setting is Option Strict On, but this setting has some implications for the behavior of code. We'll cover the recommended Option Strict On state first, followed by a demonstration of the kinds of problems you might encounter if you leave it off. To set Option Strict On, open the project's Property Pages, select the Build page, and change Compiler Defaults Option Strict to On.

When Option Strict is on, no implicit type conversion is allowed by the compiler. Again, this is what you want. Listing 2.3 demonstrates a subroutine that tries to implicitly convert a long to an integer.

Listing 2.3 Code that tries to implicitly convert a long to an integer
  1:  Sub TestStrictMode()  2:  Dim I As Integer = 5  3:  Dim L As Long = 100  4:  I = L  5:  End Sub 

If Option Strict On is true, the code in Listing 2.3 will report Option Strict disallows implicit conversions from Long to Integer because the code tries to convert the variable L, a long, to an integer.

What would happen if the long L contained a number bigger than the integer could hold and Option Strict Off were true? The answer is that the compiler wouldn't catch the possibility of the extra bits in the long potentially overflowing the available bits in the integer, resulting in the possibility of an unhandled exception at runtime rather than an error at compile time. Modifying line 3 of Listing 2.3 to initialize L to 100 billion100,000,000,000and running with Option Strict Off compiles, but raises the unhandled exception shown in Figure 2.1.

Figure 2.1. An unhandled OverflowException caused accidentally when a number too big to fit in an integer was assigned from a long.

graphics/02fig01.jpg

Note

It's your personal choice whether you like to drive without a seatbelt or not. The same is true for how you operate when you're programming. If you like implicit variable declaration and find yourself looking for a pernicious bug, try turning on Option Explicit and Option Strict and rebuilding. These two options may help you bubble defects up to compile time.


Option Strict only allows widening type conversions. With Option Strict On you can assign an integer to a long, which is fine because the long has a sufficient number of bits to hold the integer value, but the reverse is not true. With Option Strict On, you may not assign a long to an integera narrowing type conversionwithout an explicit type cast. A cast is your way of programmatically indicating that you're aware of the overflow risk, but you believe that the value of the long in this situation will fit into the number of bits available in an Integer. (See the section later in this chapter on "Type Conversion Functions" for more information.)

Option Strict On prohibits late binding, operations on the Object type other than equality, inequality, TypeOf...Is and Is tests. You are required to declare the type of a variable using the As clause with Option Strict On, too.

Option Private Module

Option Private is no longer supported in Visual Basic .NET. Option Private kept other code from using code in your VB6 modules, but VB .NET doesn't need it.

Visual Basic .NET supports access specifiers. You can add the private access specifier to members of a module to keep outside code from using those members . (Refer to Chapter 7, "Creating Classes," for more on information hiding and access specifiers.)

Option Base

Option Base is no longer supported. Option Base in VB6 allowed you to indicate whether arrays started at 0 or 1. For example,

 Dim MyArray(10) As Integer 

in VB6 allocated a 10-element array of integers if Option Base 1 were set and an 11-element arrayindexed from 0 to 10if Option Base 0 were set.

Visual Basic .NET, for all intents and purposes, uses an Option Base of 0.

It's worth pointing out that things like the number of array elements will cause minor but annoying stumbling blocks for developers initially. VB .NET beta 1 and early beta 2 supported 0 to n -1 element arrays, where n was expressed as the number of elements. In April 2001, Microsoft made some concessions to VB6 developers; returning arrays to behave like Option Base 0 with indexes 0 to n, or having n +1 elements, was one such concession.

Further confusion might exist because books were written based on the beta software and published before these concessions were made. Fortunately the authors of those beta books added disclaimers that should be taken to heart. Read the section on arrays later in this chapter for examples of VB .NET arrays.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

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