Section 7.10. Option Strict and Data-Type Conversions


7.10. Option Strict and Data-Type Conversions

Visual Basic provides several options for controlling the way the compiler handles types. These options can help you eliminate such errors as those caused by narrowing conversions. The first option is Option Explicit, which is set to On by defaultit has been enabled in the programs we created in Chapters 26. Option Explicit forces you to explicitly declare all variables before they are used in a program. Forcing explicit declarations eliminates various errors. For example, when Option Explicit is set to Off, the compiler interprets misspelled variable names as new variables and implicitly declares them to be of type Object. (Class Object is the base type of all types in Visual Basic. We will discuss class Object in more detail in Chapter 10.) This creates subtle errors that can be difficult to debug. To set Option Explicit to Off, double click My Project in Solution Explorer. This opens a window that allows you to change the properties of the project. In this window, click the Compile tab (Fig. 7.7), then select the value Off from the Option Explicit drop-down list. If Option Explicit is Off, any undeclared variables are implicitly declared as being of type Object. Note that all variables have a type regardless of whether Option Explicit is On, so Visual Basic is known as a strongly-typed language.

Figure 7.7. Modifying Option Strict and Option Explicit.


A second option, which defaults to Off, is Option Strict. Visual Basic provides Option Strict as a means of increasing program clarity and reducing debugging time. When set to On, Option Strict causes the compiler to check all conversions and requires you to perform an explicit conversion for all narrowing conversions that could cause data loss (e.g., conversion from Double to Integer) or program termination (e.g., conversion of a String, such as "hello", to type Integer). To set Option Strict to On, double click My Project in the Solution Explorerthis opens a window that allows you to change the properties of the project. In that window, click the Compile tab and select the value On in the Option Strict drop-down list (Fig. 7.7). An explicit conversion is a conversion that uses a cast operator or a method to force a conversion to occur. In Chapter 11 we discuss using operators known as cast operators to perform explicit conversions. Explicit conversion enables you to "take control" from the compiler. You essentially say, "I know this conversion might cause loss of information, but for my purposes here, that's fine."

The methods of class Convert can be used to explicitly convert data from one type to another. The name of each conversion method is the word To, followed by the name of the type to which the method converts its argument. For instance, to convert a String input by the user to type Double (to be stored in variable number of type Double), we use the statement

 number = Convert.ToDouble(Console.ReadLine()) 


Likewise, many of the primitive types have a Parse method that they can use for conversions. For instance, method Double.Parse converts a string representation of a number to a value of type Double.

When Option Strict is Off (as it has been for all the code examples so far in this book), Visual Basic performs such type conversions implicitly, and thus you may not realize that a narrowing conversion is being performed. If the data being converted is incompatible with the new type, a runtime error occurs. Option Strict draws your attention to narrowing conversions so that they can be eliminated or handled properly.

Error-Prevention Tip 7.3

From this point forward, all code examples have Option Strict set to On.


Setting Option Strict to On applies the change globally, to the entire project. You also can enable Option Strict within an individual code file by typing Option Strict OnOption statements must be placed at the start of the file above any declarations or Imports statements.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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