tutorial

Page 99

5.3 Features of VB.NET Option Explicit and Option Strict Option Explicit and Option Strict are compiler options that can be globally assigned to a project and are interpreted at compile time. Setting these options enables programmers to resolve some of the errors (e.g. typological errors) at compile time and thus prevent runtime errors. Option Explicit Option Explicit was a feature of VB 6.0 and it has been made a part of .NET environment too. This option can be used only at the module level. When this option is turned on, it forces explicit declaration of variables in that module. This option can be turned "On" or "Off". When it is not specified, by default, it is set to "Off".

Syntax: Option Explicit [On / Off] When it is set to "On", it checks for any undeclared variables in the module at compile time. If any undeclared variable is found, it generates a compile time error since the compiler would not recognize the type of the undeclared variable. When it is set to "On", variables can be declared using Dim, Public, Private or ReDim statements. Setting this option to "On" helps programmers do away with any typological errors in the code. When it is set to "Off", all undeclared variables are considered to be of type Object. It is preferable to set this option to "On". Option Strict Visual Basic language in general does not require explicit syntax to be used when performing operations that might not be optimally efficient (e.g. late binding) or that might fail at run time (e.g. narrowing conversions). This permissive semantics often prevents detection of coding errors and also affects the performance of the application. VB.NET enables a programmer to enforce strict semantics by setting this option to "On". When used, this option should appear before any other code. This option can be set to "On" or "Off". If this statement is not specified, by default, it is set to "Off".

Syntax: Option Strict [On / Off] When it is set to "On", it disallows any narrowing conversions to occur without an explicit cast operator, late binding and does not let the programmer omit "As" clause in the declaration statement. Since setting it to "On" requires explicit conversion, it also


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.