Skip to Content
VB.NET Language in a Nutshell, Second Edition
book

VB.NET Language in a Nutshell, Second Edition

by Steven Roman PhD, Ron Petrusha, Paul Lomax
April 2002
Intermediate to advanced
688 pages
19h 51m
English
O'Reilly Media, Inc.
Content preview from VB.NET Language in a Nutshell, Second Edition

Name

MyBase Keyword

Syntax

MyBase

Description

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.

Rules 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 CTestClass
    ...
    End Class
    
    Public Class CTestClass2
       Inherits CTestClass
    
       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 Object class.

  • MyBase cannot be used to call Private class members.

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

Programming Tips and Gotchas

  • MyBase is commonly used to call back into the 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(...)

VB.NET/VB 6 Differences

The MyBase keyword is new to VB.NET.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

VB .NET Language in a Nutshell

VB .NET Language in a Nutshell

Steven Roman PhD, Ron Petrusha, Paul Lomax

Publisher Resources

ISBN: 0596003080Supplemental ContentCatalog PageErrata