Option statements are essentially compiler directives that are placed at the top of a code file (so a form, class, or module) to control how the compiler builds the code. In Visual Basic 6.0 there were four of these statements (Option Explicit, Option Compare, Option Private Module, and Option Base), but only two of those statements are included in Visual Basic .NET (Option Explicit and Option Compare). In addition to removing two of the existing statements, a completely new Option statement has been added, Option Strict.
IT IS ALL ABOUT THE FRAMEWORKThe real changes between previous versions of VB and VB.NET are not in the language but instead in many of the new Framework features. With the new models for Windows Forms, Web Development, Data Access, XML, and others, the Framework has replaced existing COM libraries (such as ADO and MSXML) or built-in features of Visual Basic 6.0. We will cover each of those areas in the following chapters. |
Adding this statement to a code file in Visual Basic 6 forced you to declare all of your variables before they could be used, which helped you to avoid many confusing bugs due to mistyping a variable name. Without it, you could write code like Listing 2.1.
Dim myName As String Private Sub Form_Load() myNme = "Duncan Mackenzie" End Sub Private Sub Command1_Click() MsgBox myName End Sub |
You would not get a single error, at compile time or at runtime, but your MsgBox window would always be empty. This type of problem, hidden inside a much more complex project, can be very difficult to debug. With Option Explicit at the top of the file, the attempt to use a variable called myNme (because it was never declared) would result in a compile error and you would have to fix it before you could run your application.
In Visual Basic .NET, Option Explicit works in the exact same fashion, but with two improvements. First, the concept of choosing a default setting (it was always off by default in Visual Basic 6) is now available; you can pick whether Option Explicit should be on or off by default in the properties dialog box for your project (see Figure 2.1).
Whatever setting you choose in this dialog box will be used for any code files (in that project) that do not have an Option Explicit line at the top. Now that there is the concept of a default, the actual statement has been extended to require an additional parameter On or Off. Instead of just Option Explicit, you must now use either Option Explicit On or Option Explicit Off. Adding the On/Off parameter allows you to have a single code file that follows different rules from the project default.
A new setting in Visual Basic .NET, Option Strict reflects the new .NET preference for type safety across your code. With Option Strict On, a data type must be specified for all variable declarations (including in parameter lists for subs and functions) and “implicit” data type conversions are not allowed.
The first rule, requiring data types to be specified, is clear. For example, the code in Listing 2.2 is not allowed.
Sub Test() Dim x End Sub Private Function DoMath(ByVal myInt) End Function |
To allow the code from Listing 2.2 to compile with a setting of Option Strict On, it has to be converted to look like Listing 2.3.
Sub Test() Dim x As String End Sub Private Function DoMath(ByVal myInt As Integer) As Integer End Function |
The variable x (line 2) needs a data type, as does the myInt parameter for DoMath (line 5). DoMath (being a function) needs to indicate a data type for its return value (also line 5).
The other effect of Option Strict On is that implicit conversions are not allowed. What does that mean? Visual Basic has a heritage of loosely typed code, where the language just tried to do its best to make everything work, even if that meant handling a few little details for you. In Visual Basic 6.0 or in Visual Basic .NET with Option Strict Off, if you assign a String to an Integer, Visual Basic will try to parse the text into a numeric value. That behind-the-scenes parsing is an implicit conversion, you never had to state that you wanted it to take place. Visual Basic just did it automatically. See whether you can spot all of the implicit conversions that occur in the Visual Basic 6 code shown in Listing 2.4.
By my count, there are four implicit conversions occurring in this code:
myResult = Text1 + Text2 (line 5) requires the conversion of each TextBox's contents from a String to a numeric value.
Text3 = myResult (line 6) requires the conversion of an Integer (myResult) back into a String for placing into the TextBox.
Text3 >= myHighValue (line 8) either requires a conversion of an Integer (myHighValue) into a String for comparing with the contents of the TextBox, or the conversion of String into an Integer. The fact that it could be either (actually depending on how you arrange your If statement) is one of the underlying uncertainties with implicit conversion!
MsgBox "Result is >= " & myHighValue (line 9) requires another Integer to String conversion for myHighValue to be able to concatenate it with the rest of the message text.
If you choose to make Option Strict On the default for your project, or if you set it just for this code file, none of those implicit conversions are allowed. In each instance of an implicit conversion, you will need to explicitly perform the desired conversion yourself to allow this code to even compile. Listing 2.5 uses the familiar data type conversion functions from Visual Basic 6 to wrap each String value in CInt() when I needed to convert it to an Integer, and wrap each Integer in CStr() when I needed to convert them to Strings.
Note that to convert the code from Listing 2.5 to Visual Basic .NET I had to change more than just the data type conversion elements, I also had to add the .Text after each TextBox reference. I will explain why that change was necessary in the “No More Default Properties” section later in this chapter.
The use of implicit conversions was not a configurable option in Visual Basic prior to .NET; implicit conversions occurred all the time, potentially causing problems without any warning at all. Now you can specify Option Strict On/Off at the top of a specific code file, and (as you can see in Figure 2.1) it can also be set at the project level just like Option Explicit.
This statement, which toggles between two styles of string comparison (Text and Binary), works the same in Visual Basic .NET as it did in earlier versions. The only difference is that it is now a configurable project level option in addition to being configurable at the file level. Binary indicates that Strings should be compared based on their underlying ASCII/Unicode values, making A < Z < a < z (because the uppercase characters are located at lower positions in the character table than the lowercase characters). Text indicates that strings should be compared using a system that is independent of the underlying character codes and is therefore case insensitive (A=a, Z=z, and so on). Binary comparison is the default, but Text comparison is more understandable to a human user of your system. You should use the Text setting whenever you are sorting text for display.
Given that each of these Option settings can be set at the project level and/or in the individual code file, what is the best way to ensure that your desired settings are always the default? Personally, I want to code with Option Explicit On and Option Strict On, but I often forget to include those lines in my code or to set the appropriate project level settings; so I needed a way to make these two settings the default for all the new projects I start or new code files I create. If you want to achieve the same thing, the key is to look at the template files used by Visual Studio .NET when it creates new projects or new code files. The individual file templates are located under C:Program FilesMicrosoft Visual Studio .NETVb7VBWizards on my system, the exact location of yours will depend on where you installed VS .NET. Adding the desired Option lines to the top of each of the .VB code files will cause those lines to appear in each new file you create through the IDE's Add [Item] menu options. Templates for the project files are also available, under the same location in your VS .NET installation and you can modify those files if you want to set the default Option settings at the project level. See this book's Web site for more information on the details of setting up these defaults on your own system.
18.118.37.154