
OOP Development in Visual Basic | 43
OOP
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
MyBase, MyClass, and Me
When working with derived classes, there are times when references to a member
may be somewhat ambiguous; a member name may exist in both the derived class
and the base class. Visual Basic provides special keywords to help alleviate this
ambiguity.
The
MyBase keyword 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
This will resolve any ambiguity if the derived class also has a member of the same
name. The
MyBase keyword can also be used to create an instance of the base class
through its constructor:
MyBase.New(...)
The MyBase keyword cannot be used to access Private members of the base class,
as they are inaccessible from derived classes.
If a class is derived from a chain of base and derived classes,
MyBase looks first to
the closet “parent” class in the chain for a matching member (including a
matching signature). If a match is not found, VB continues up the chain until the
root class, which is always System.Object.
The keywords
Me and MyClass both provide a reference to the local class (the class
in which the current code resides), but they exhibit slight differences. Consider a
class named ...