Option Statements


A few code examples ago, you saw that Visual Basic would supply a default assignment to a variableat least for value typesif you neglected to include one. In certain cases, Visual Basic will also supply the declaration if you leave it out. In the statement:

brandNewValue = 5 


if there is no related Dim statement that defines brandNewValue, Visual Basic will declare the variable on your behalf, assigning it to the Object data type. Don't let this happen to you! You don't know what kind of trouble you will have if you allow such practices in your code. You will quickly find your code filled with mysterious logic bugs, esoteric data issues, recurrent head lice, and so on.

The problem is that Visual Basic will not complain if you mistype the name of your auto-declared variable. Left unchecked, this practice could lead to code like this:

brandNewValue = 5 MsgBox(brandNewVlaue) 


My, my, my, look at that spelling mistake on the second line. What? Visual Basic compiled without any error? And now your message box displays nothing instead of 5? Such trauma could be avoided by judicious use of the Option statements included in the Visual Basic language. There are three such statements.

  • Option Explicit On. This statement forces you to declare all variables using Dim (or a similar statement) before use. It's possible to replace "On" with "Off" in the statement, but don't do it.

  • Option Strict On. Visual Basic will do some simple data conversions for you when needed. For instance, if you assign a 64-bit Long data value to a 32-bit Integer variable, Visual Basic will normally convert this data to the smaller size for you, complaining only if the data doesn't fit. This type of conversiona narrowing conversionis not always safe because the source data will sometimes fail to fit in the destination. (A widening conversion, as with storing Integer data in a Long, always works, because the destination can always hold the source value.) The Option Strict On statement turns off the automatic processing of narrowing conversions. You will be forced to use explicit conversion functions to perform narrowing conversions. This is good, because it forces you to think about the type of data your variables will hold. You can replace "On" with "Off" in this statement, but if I've warned you once, I've warned you twice: Don't even try it.

  • Option Compare Binary and Option Compare Text. These two variations of the Option Compare statement instruct your code to use specific sorting rules for certain string comparison features. In general, "Binary" comparison is case-sensitive, while "Text" comparison is not. It's up to you which method you want to use; the default is "Binary."

    These statements appear at the top of each source code file in your project, before any other code.

    Option Explicit On Option Strict On 

Or, to save on precious disk space, set default values that apply to your entire project through the project's properties. In Visual Studio, select the Project Properties menu command. On the project's properties window that appears, select the Compile tab, and set your default choices for the Option explicit, Option strict, and Option compare fields (see Figure 2-2).

Figure 2-2. Options, options everywhere





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

Similar book on Amazon

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