Datasheet
From the properties page Option Explicit, Option Strict, Option Compare, and Option Infer can be set to
either On or Off for your project. Visual Studio 2010 makes it easy for you to customize specifi c compiler
conditions for your entire project. However, as noted, you can also make changes to the individual
compiler checks that are set using something like Option Strict.
Notice that as you change your Option Strict settings in particular, the notifi cations with the top few
conditions are automatically updated to refl ect the specifi c requirements of this new setting. Therefore, you
can literally create a custom version of the Option Strict settings by turning on and off individual compiler
settings for your project. In general, this table lists a set of conditions that relate to programming practices
you might want to avoid or prevent, and which you should defi nitely be aware of. The use of warnings for
the majority of these conditions is appropriate, as there are valid reasons why you might want to use or
avoid each but might also want to be able to do each.
Basically, these conditions represent possible runtime error conditions that the compiler can ’ t detect in
advance, except to identify that a possibility for that runtime error exists. Selecting a Warning for a setting
bypasses that behavior, as the compiler will warn you but allow the code to remain. Conversely, setting a
behavior to Error prevents compilation; thus, even if your code might be written to never have a problem,
the compiler will prevent it from being used.
An example of why these conditions are noteworthy is the warning of an Instance variable accessing a
Shared property. A Shared property is the same across all instances of a class. Thus, if a specifi c instance
of a class is updating a Shared property, then it is appropriate to get a warning to that effect. This action
is one that can lead to errors, as new developers sometimes fail to realize that a Shared property value is
common across all instances of a class, so if one instance updates the value, then the new value is seen by
all other instances. Thus, you can block this dangerous but certainly valid code to prevent errors related to
using a Shared property.
As noted earlier, option settings can be specifi c to each source fi le. This involves adding a line to the top of
the source fi le to indicate to the compiler the status of that
Option . The following lines will override your
project ’ s default setting for the specifi ed options. However, while this can be done on a per - source listing
basis, this is not the recommended way to manage these options. For starters, consistently adding this line
to each of your source fi les is time - consuming and potentially open to error:
Option Explicit On
Option Compare Text
Option Strict On
Option Infer On
Most experienced developers agree that using Option Strict and being forced to recognize when type
conversions are occurring is a good thing. Certainly, when developing software that will be deployed in a
production environment, anything that can be done to help prevent runtime errors is desirable. However,
Option Strict can slow the development of a program because you are forced to explicitly defi ne each
conversion that needs to occur. If you are developing a prototype or demo component that has a limited life,
you might fi nd this option limiting.
If that were the end of the argument, then many developers would simply turn the option off and forget
about it, but Option Strict has a runtime benefi t. When type conversions are explicitly identifi ed, the system
performs them faster. Implicit conversions require the runtime system to fi rst identify the types involved in a
conversion and then obtain the correct handler.
Another advantage of Option Strict is that during implementation, developers are forced to consider every
place a conversion might occur. Perhaps the development team didn ’ t realize that some of the assignment
operations resulted in a type conversion. Setting up projects that require explicit conversions means that the
resulting code tends to have type consistency to avoid conversions, thus reducing the number of conversions
in the fi nal code. The result is not only conversions that run faster, but also, it is hoped, a smaller number
of conversions.
Option Infer is a powerful feature. It is used as part of LINQ and the features that support LINQ, but it affects
all code. In the past, you needed to write the
AS < type > portion of every variable defi nition in order to have a
variable defi ned with an explicit type. However, now you can dimension a variable and assign it an integer or
Visual Basic Keywords and Syntax
❘
19
CH001.indd 19CH001.indd 19 4/5/10 11:56:30 AM4/5/10 11:56:30 AM