Você está na página 1de 2

3.3.

2 Class Members
In VB .NET, class modules can contain the following types of members:

Data members
This includes member variables (also called fields) and constants.
Event members
Events are procedures that are called automatically by the Common Language Runtime
in
response to some action that occurs, such as an object being created, a button
being clicked,
a piece of data being changed, or an object going out of scope.
Function members
This refers to both functions and subroutines. A function member is also called a
method. A
class' constructor is a special type of method. We discuss constructors in detail
later in this
chapter.
Property members
A property member is implemented as a Private member variable together with a
special type
of VB function that incorporates both accessor functions of the property. We
discuss the
syntax of this special property function in Section 3.3.5 later in the chapter.
Type members
A class member can be another class, which is then referred to as a nested class.

MyBase, MyClass, and Me


The keyword MyBase provides a reference to the base class from within a derived
class. If you want to
call a member of the base class from within a derived class, you can use the
syntax:
MyBase.MemberName
where MemberName is the name of the member. This will resolve any ambiguity if the
derived class
also has a member of the same name.
The MyBase keyword can be used to call the constructor of the base class in order
to instantiate a
member of that class, as in:
MyBase.New(...)
Note that MyBase cannot be used to call Private class members.

Visual Basic looks for the most immediate version in parent classes of the
procedure in question. Thus,
if Class C derives from Class B, which derives from Class A, a call in Class C to:
MyBase.AProc
first looks in Class B for a matching procedure named AProc. If none is found,
then VB looks in Class
A for a matching procedure. (By matching, we mean a method with the same argument
signature.)
The keyword MyClass provides a reference to the class in which the keyword is
used. It is similar to
the Me keyword, except when used to call a method. To illustrate the difference,
consider a class
named Class1 and a derived class named Class1Derived. Note that each class has an
IncSalary
method:
Public Class Class1
Public Overridable Function IncSalary(ByVal sSalary As Single) _
As Single
IncSalary = sSalary * CSng(1.1)
End Function
Public Sub ShowIncSalary(ByVal sSalary As Single)
MsgBox(Me.IncSalary(sSalary))
MsgBox(MyClass.IncSalary(sSalary))
End Sub
End Class
Public Class Class1Derived
Inherits Class1
Public Overrides Function IncSalary(ByVal sSalary As Single) _
As Single
IncSalary = sSalary * CSng(1.2)
End Function
End Class
Now consider the following code, placed in a form module:
Dim c1 As New Class1( )
Dim c2 As New Class1Derived( )
Dim c1var As Class1
c1var = c1
c1var.IncSalary(10000) ' Shows 11000, 11000
c1var = c2
c1var.IncSalary(10000) ' Shows 12000, 11000
The first call to IncSalary is made using a variable of type Class1 that refers to
an object of type
Class1. In this case, both of the following calls:
Me.IncSalary
MyClass.IncSalary
return the same value, because they both call IncSalary in the base class Class1.

However, in the second case, the variable of type Class1 holds a reference to an
object of the derived
class, Class1Derived. In this case, Me refers to an object of type Class1Derived,
whereas MyClass
still refers to the base class Class1 wherein the keyword MyClass appears. Thus,
Me.IncSalary
returns 12000 whereas the following:
MyClass.IncSalary
returns 10000.

Você também pode gostar