(Print this page)

Distinguishing Visual Basic Me, My, MyBase, and MyClass
Published date: Monday, June 25, 2012
On: Moer and Éric Moreau's web site

This article is oriented toward VB developer this month. I’ll try to explain how to differentiate these 4 confusing (at first) keywords.

No C# code this month

I have been providing C# code for my articles for a long time now but not this month. The reason is that MyClass has no equivalent in that language. MyClass is purely VB.

The easy one: My

Let’s start with the easy one: My. The My keyword was introduced in the VB language with the .Net Framework 2.0 (TODO) and is simply a shortcut to often used .Net Framework classes. I have already covered this keyword in June 2007.

The obvious one: Me

This is the most often used keyword even if you don’t even always write it yourself because it is implicit from within an instance. In theory, the Me keyword is used to access the current instance.

The common one: MyBase

Sometimes, you don’t want to access to the current instance but you would prefer to refer to the base instance when you have overridden or shadowed a member. We very often see this keyword in constructors. We want to do everything the base constructor is doing and add something specific to the current instance in the derived class.

Notice that if your classes inherits many levels deep, MyBase will call the members of the immediate base class (the one just above) and won’t go until the root.

The obscure one: MyClass

MyClass behaves mostly like Me. The only exception is that it will force to use the current instance of the class (and not any of its overrides) as if the members were declared as NotOverridable. In other words, MyClass makes sure that the member of the current class is dispatched and not the overridden member.

The demo

Now that we have the theory, let’s demo how it affects your code.

We start with a very simple base class:

Public Class BaseClass
 
    Public Overridable Function TestFunction() As String
        Return "Base class function used"
    End Function
 
    Public Function TestMe() As String
        Return Me.TestFunction()
    End Function
 
    Public Function TestMyClass() As String
        Return MyClass.TestFunction()
    End Function
 
End Class

This base class contains a function (TestFunction) that returns a string indicating that it was contained in the base class. This function will be overridden in the derived class with a different message.

This base class also contains 2 functions (TestMe and TestMyClass) which are calling the TestFunction member with a different qualifier. We will see the result when running the UI.

Then we have the derived class:

Public Class DerivedClass
    Inherits BaseClass
 
    Public Overrides Function TestFunction() As String
        Return "Derived class function used"
    End Function
 
    Public Function TestMyBase() As String
        Return MyBase.TestFunction()
    End Function
 
End Class

The TestFunction here overrides the method of the same signature from the base class and returns a different message.

The other method (TestMyBase) will be used to demonstrate the MyBase keyword. This keyword was not really testable from the base class.

Finally, we can create the UI. To test these 3 qualifiers, I just added a button and a listbox to a form. This code is what we will work with:

Public Class Form1
 
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim x1 As New BaseClass
        Dim x2 As New DerivedClass
 
        With ListBox1.Items
            .Clear()
 
            .Add("Base Class")
            .Add("Me - " + x1.TestMe())
            .Add("MyClass - " + x1.TestMyClass())
 
            .Add("")
            .Add("Derived Class")
            .Add("Me - " + x2.TestMe())
            .Add("MyClass - " + x2.TestMyClass())
            .Add("MyBase - " + x2.TestMyBase())
        End With
    End Sub
 
End Class

In this code, we first declare x1 as an instance of the base class.

Using x1, we call the TestMe and the TestMyClass methods. The returned message for both calls will be the one from the base class because the derived class is never used anywhere. So far, there is no surprise.

Then, the x2 variable is declared as an instance of the derived class.

  • We first call the TestMe method. This method only exists in the base class. This method in turns calls the TestFunction method with the Me qualifier. Me refers to the current instance of the class (which is DerivedClass). Even if the TestMe method is in the base class, the TestFunction method of the derived class will be correctly called.
  • Then, we call the TestMyClass method. This method only exists in the base class. Because this method (from the base class) calls the TestFunction with the MyClass qualifier, the base instance is used. That explains why the message from the base class is returned.
  • Finally, we call the TestMyBase method. This method only exists in the derived class. This time the MyBase qualifier is used forcing the base class version of the method to be called.

 

Figure 1: The demo application in action

Conclusion

A simple qualifier sometimes changes the complete execution. It is really important for every VB developers to fully understand the scope of Me, MyClass, and MyBase.


(Print this page)