There are two major ways to deal with errors:
- Prevent them
- Handle them well when they can't be prevented
The major problem that causes errors in programs is data that isn't what the program expected. The major way to solve this problem is to ensure that the data that you expect to process is actually the data that is received. Four "compiler options" help ensure that the data types in your program are exactly what your program expects.
This is important because a data type determines how information is stored and processed. These options help catch errors when you're writing your program that could create big problems for people using your programs after they get into production. For example, processing intended for an integer number can crash if a decimal number is used. In addition, a data type could also be one that you define yourself. In the Signature Block program, the SigBlockStruct structure that we defined in an earlier segment is a user defined data type.
There are four optional statements to help you achieve this:
- Option Explicit
- Option Strict
- Option Infer
- Option Compare
If you use them, these statements have to appear before any other source code. And all of them can be set on or off by default in VB.NET. (Select Tools > Options, then Projects and Solutions > VB Defaults in VB Express.)
Option Explicit
Option Explicit tells VB.NET to require a Dim or ReDim statement to declare all of your variables. If you don't declare your variables, you're taking a step backward to the way VB used to work in VB6. If you don't declare a variable, then VB.NET creates it as an Object datatype, which runs slower and takes more memory. Option Explicit On is the default setting.
Here's an example of what can happen if you don't have this option.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Option Strict
Option Strict is a level above Option Explict because it restricts how you can convert one type of data into another. When Option Strict is on, implicit data type conversions are only allowed for widening conversions. In general, this means that VB.NET won't allow you to convert a variable that can contain more into one that can't contain as much. The purpose is to prevent possible data loss. Leaving Option Strict set to off has also been called "Option Slow" because the compiler will then include extra code for type conversion.
Option Infer
Option Infer is a new compiler option in VB.NET 2008 and it not only helps prevent errors, it also changes what actually happens when your code is compiled. In addition, the default is different when code is written originally in VB.NET 2008 compared to code that was upgraded from an earlier version. (To see this, you have to view the options under the Compile tab of the project's Properties window. Looking at it using "Tools > Options > Projects and Solutions > VB Defaults" gives you the wrong answer.) It's tricky!
This option lets you declare variables without explicitly stating a data type. The primary example is something like this:
Dim myVar = "Whatever"
Debug.WriteLine(myVar.GetType)
The Immediate window reports that myVar is of type System.String.
But consider this example. In VB.NET 2005, this code works:
Dim myVar = 12345
Debug.WriteLine( _
"After Assignment to Integer: " _
& myVar.GetType.ToString)
myVar = "whatever"
Debug.WriteLine( _
"After Assignment to String: " _
& myVar.GetType.ToString)
After Assignment to Integer: System.Int32
After Assignment to String: System.String
In VB.NET 2008, not only is the error not caught in the compile, but it could actually crash in production. (Because in VB.NET 2005, the variable is an Object data type which can hold anything. In VB.NET 2008, the data type becomes an Integer due to the new Option Infer.) So the option can create a step backward, too.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
You should also keep in mind that Option settings are by file. As the illustration below shows, setting an option in one file doesn't change the setting for any other file.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Beyond these compiler supported ways to make sure your code is correct, you should also have lots of checking on data to ensure that it fits the parameters of your program. In large scale business systems, this is often done in completely separate data validation layers just to protect the more critical processing layers. Regular Expressions can help verify that String data is correct. An introduction to using them can be found in this article.
Although your program should do everything possible to prevent an error, sometimes you have to program to respond to errors that can't be prevented. On the next page, we learn about Structured Exception Handling to do that.
Option Compare
Option Compare doesn't do anything like the previous three options and is only included here for completeness. It can have the values Binary or Text and controls whether string comparisons are based on an internal binary representation of strings or a case-insensitive text sort order determined by your system's locale.

