Sunteți pe pagina 1din 4

Exit Statement

Syntax
Exit Do
Exit For
Exit Function
Exit Property
Exit Select
Exit Sub
Exit Try
Exit While
Description
Prematurely exits a block of code
Rules at a Glance
Exit Do
Exits a Do...Loop statement. If the current Do...Loop is within a nested Do...Loop,
execution continues with the next Loop statement wrapped around the current one. If,
however, the Do...Loop is standalone, program execution continues with the first line of
code after the Loop statement.
Exit For
Exits a For...Next loop or a For Each...Next statement. If the current For...Next is
within a nested For...Next loop, execution continues with the next Next statement
wrapped around the current one. If, however, the For...Next loop is standalone, program
execution continues with the first line of code after the Next statement.
Exit Function
Exits the current function. Program execution is passed to the line following the call to the
function.
Exit Property
Exits the current property procedure. Program execution is passed to the line following the call
to the property.
Exit Select
Immediately exits a Select Case construct. Execution continues with the statement
immediately following the End Select statement.
Exit Sub
Exits the current sub procedure. Program execution is passed to the line following the call to
the procedure.
Exit Try
Immediately exits a Try...Catch block. Program execution proceeds with the Finally
block, if it is present, or with the statement following the End Try statement.
Exit While
Immediately exists a While loop. Program execution proceeds with the code following the
End While statement. If Exit While is within a nested While loop, it terminates the loop at
the level of nesting in which Exit While occurs.
Option Compare Statement
Syntax
Option Compare {Binary | Text}
Description
Used to set the default method for comparing string data
Rules at a Glance
• When Option Compare is not used in a module, the default comparison method is Binary.
• When Option Compare is used, it must appear at the start of the module's declarations
section, before any procedures.
• Binary comparison —the default text comparison method in Visual Basic—uses the internal
binary code of each character to determine the sort order of the characters. For example, "A"
< "a".
• Text comparison uses the locale settings of the current system to determine the sort order of
the characters. Text comparison is case insensitive. For example, "A" = "a".
Option Explicit Statement
Syntax
Option Explicit [On | Off]
Description
Use Option Explicit to generate a compile-time error whenever a variable that has not been
declared is encountered.
Rules at a Glance
• The Option Explicit statement must appear in the declarations section of a module before
any procedures.
• In modules where the Option Explicit statement is not used, any undeclared variables are
automatically cast as Objects.
• The default is Option Explicit On. In other words, the statement:
Option Explicit
is equivalent to:
Option Explicit On
Programming Tips and Gotchas
• It is considered good programming practice to always use the Option Explicit statement.
The following example shows why:
• 1: Dim iVariable As Integer

• 2: iVariable = 100
• 3: iVariable = iVariable + 50
4: MsgBox iVariable
In this code snippet, an integer variable, iVariable, has been declared. However, because
the name of the variable has been mistyped in line 3, the message box shows its value as
only 50 instead of 150. This is because iVarable is assumed to be an undeclared variable
whose value is 0. If the Option Explicit statement had been used, the code would not
have compiled, and iVarable would have been highlighted as the cause.
• For an ASP.NET page, you use the @ PAGE directive rather than Option Explicit to
require variable declaration. Its syntax is:
<%@ Page Language="VB" Explicit=true|false %>
By default, Explicit is true in ASP.NET pages.
You can also use the <system.web> section of the WEB.Config file to require variable
declaration for an entire virtual directory or ASP.NET application by adding an explicit attribute
to the compliation section. Its syntax is:
<compliation strict="true|false">
In both cases, true corresponds to Option Explicit On, and false corresponds to
Option Explicit Off.
Option Strict Statement
Syntax
Option Strict [On | Off]
Description
Option Strict prevents VB from making any implicit data type conversions that are
narrowing since
narrowing conversions may involve data loss. For example:
Dim lNum As Long = 2455622
Dim iNum As Integer = lNum
converts a Long (whose value can range from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807) to an Integer (whose value can range from 2,147,483,648 to
2,147,483,647). In this case, even though no data loss would result from the narrowing, Option
reasoning here is that, although particular narrowing operations may not lose data, there is
always the
potential for data loss when working with variables—that is, with symbolic representations of
numbers
whose values are allowed to vary.
Rules at a Glance
• If the Option Strict statement is not present in a module, Option Strict is Off.
• The default is Option Strict On. In other words, the statement:
Option Strict On
is equivalent to the statement:
Option Strict
• The Option Strict statement must appear in the declarations section of a module before
any code.
• Option Strict On disallows all implicit narrowing conversions.
• Option Strict On also causes errors to be generated for late binding, as well as for any
undeclared variables, since Option Strict On implies Option Explicit On.
Programming Tips and Gotchas
• Although the setting of Option Strict has no effect on BCL data types, BCL data types
disallow implicit narrowing conversions.
• Explicit narrowing conversions are not affected by Option Strict. However, if data loss
does occur as a result of an explicit conversion, an OverflowException exception is generated.
• One of the most commonly overlooked narrowing conversions is the use of "wider" arguments
in function, procedure, and method calls. Passing a Long to an Integer parameter, for example,
is an implicit narrowing conversion that Option Strict does not allow.
• In many cases, Option Strict On disallows seemingly "safe" conversions because it
interprets literal values in unexpected ways. For example, the statement
Dim decNum As Decimal = 10.32
generates a compiler error because 10.32 is interpreted as a Double, and implicit conversions
from Double to Decimal are not allowed. You can correct this compiler error with a statement
like:
Dim decNum As Decimal = 10.32D
• Setting Option Strict On is highly recommended.
• For an ASP.NET page, you use the @ Page directive rather than Option Strict to control
strict type checking. Its syntax is:
<%@ Page Language="VB" Strict=true|false %>
By default, Strict is false in ASP.NET pages.
You can also use the <system.web> section of the WEB.Config file to control strict type
checking for an entire virtual directory or ASP.NET application by adding a strict attribute to
the compilation section. Its syntax is:
<compilation strict="true|false">
In both cases, true corresponds to Option Explicit On, and false corresponds to
Option Explicit Off.

Shadows

Indicates that a derived class member is hidden if its class is assigned to an instance of its base
class.
Calls to the shadowed method when made through the base class see the base class
implementation rather than the shadowed implementation.

Shadowing
Overloading allows you to add new versions of the existing methods as long as their parameter
lists are
different. Overriding allows your subclass to entirely replace the implementation of a base
class method
with a new method that has the same method signature. As you’ve just seen, you can even
combine

these concepts not only to replace the implementation of a method from the base class but
also to simultaneously
overload that method with other implementations that have different method signatures.
However, any time you override a method using the Overrides keyword, you are subject to the
rules
governing virtual methods—meaning that the base class must give you permission to override
the
method. If the base class doesn’t use the Overridable keyword, you can’t override the method.
Sometimes you may need to override a method that is not marked as Overridable, and
shadowing
allows you to do just that.
The Shadows keyword can also be used to entirely change the nature of a method or other
interface element
from the base class, although that is something which should be done with great care, since it
can
seriously reduce the maintainability of your code. Normally, when you create an Employee
object, you
expect that it can only act as an Employee, but also as a Person since Employee is a subclass of
Person.
However, with the Shadows keyword, you can radically alter the behavior of an Employee class
so that
it doesn’t act like a Person. This sort of radical deviation from what is normally expected invites
bugs
and makes code hard to understand and maintain.
Shadowing methods is very dangerous and should be used as a last resort. It is primarily useful
in cases
where you have a preexisting component such as a Windows Forms control that was not
designed for
inheritance. If you absolutely must inherit from such a component, you may need to use
shadowing to
“override” methods or properties. There are serious limits and dangers, but it may be your only
option.

S-ar putea să vă placă și