MyBase Keyword


MyBase Keyword

Syntax

     MyBase 

Description

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 

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.

Usage at a Glance

  • MyBase will call through the chain of inherited classes until it finds a callable implementation. For example, in the code:

         Public Class TestClass        ...     End Class     Public Class TestClass2        Inherits TestClass        Public Function ShowType(  ) As Type           Return MyBase.GetType        End Function     End Class 

    the call to ShowType is eventually resolved as a call to Object.GetType, since all classes are ultimately derived from the System.Object class.

  • MyBase cannot be used to call Private class members in the base class.

  • MyBase cannot be used to call base class members marked as MustOverride.

  • MyBase is commonly used to call an overridden member from the member that overrides it in the derived class.

  • The MyBase keyword can be used to call the constructor of the base class to instantiate a member of that class, as in:

         MyBase.New(...) 

Version Differences

The MyBase keyword is new to VB under .NET.

See Also

Me Keyword, MyClass Keyword




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net