If you haven't had exactly the right requirement in one of your programs, you might not even be aware that the MyClass keyword exists. Most people need to use the much more common Me and MyBase keywords every now and then, but MyClass covers a fairly specialized and unique requirement. This Quick Tip explains what it is.
According to Microsoft, these three keywords are "object variable like". In other words, in most cases, Me, MyBase, and MyClass act just like objects. You use them along with methods and properties in a syntax like this:
MyClass.<some method or property>
To get started, Me refers to "the specific instance of a class or structure in which the code is currently executing." Most programmers run into it because it's required when referring to the form in a Windows Forms based application:
--------
Click Here to display the illustration
--------
MyBase refers to "the base class of the current instance of a class." So when an instance of a class inherits another class, MyBase refers to the parent, like this:
Public Class baseClass
Public theBaseClassObject As String = "Whatever"
End Class
Class derivedClass
Inherits baseClass
Public Sub ReferenceMyBaseObject()
Console.WriteLine(MyBase.theBaseClassObject)
End Sub
End Class
This code uses the baseClass object as inherited in derivedClass:
Dim instantiateDerivedClass As New derivedClass
instantiateDerivedClass.ReferenceMyBaseObject()
A more complete article that introduces these keywords and more can be found at:
Classes - Base Classes and Derived Classes. And this link had an explanation of Overloads versus Overrides.
It would seem that all contingencies have been covered, but there's one that hasn't yet. Suppose a derived class method or property overrides a property with the same name in a base class. Here's a method being overridden.
Public Class baseClass
Public Overridable Sub theMethod()
' baseClass code goes here
End Sub
End Class
Class derivedClass
Inherits baseClass
Public Overrides Sub theMethod()
' new derivedClass code goes here
' this code runs, not baseClass code
MyBase.theMethod()
End Sub
End Class
Visual Studio AutoComplete adds the statement ...
MyBase.theMethod()
... automatically because you will normally want to add some new processing and then continue with the processing that is already there in the base class. In general, you won't know what processing is in the base class. (It's often a code library that you don't have access to.) Overrides is explained in more detail in this article: Overrides in VB.NET.
Because the method is overridden in the derived class, the code there is executed rather than the base class code. (That's why MyBase.theMethod is usually a good idea.) But what if the base class author didn't want any code in a derived class to run for some reason. (For example, suppose it's a security application and derived classes can implement their own security by overriding, but there are some fundamental methods that must not be overridden.) How do you make sure that happens?
The answer is MyClass. As Microsoft's documentation states, "all method calls on it are treated as if the method were NotOverridable." Microsoft has a pretty good example of a method, but it also works with properties. Using an example developed in the article Overriding ToString, this code shows how MyClass can be used to ensure that a property isn't overridden. Here's the base class code:
Public Class baseClass
Dim theDate As Long
Public Overridable ReadOnly Property _
theProperty As Long
Get
Dim aYear As Integer = Now.Year
Dim aMonth As Integer = Now.Month
Dim aDay As Integer = Now.Day
theDate =
DateSerial(
aYear, aMonth, aDay
).Ticks + 999
Return theDate
End Get
End Property
Public Sub useMe()
Console.WriteLine(
"Property using Me: " &
theProperty)
End Sub
Public Sub useMyClass()
Console.WriteLine(
"Property using MyClass: " &
MyClass.theProperty)
End Sub
End Class
Here's the derived class code:
Class derivedClass
Inherits baseClass
Dim theDate As Long
Public Overrides ReadOnly Property _
theProperty As Long
Get
Dim aYear As Integer = Now.Year
Dim aMonth As Integer = Now.Month
Dim aDay As Integer = Now.Day
theDate = DateSerial(
aYear, aMonth, aDay
).ToBinary
Return theDate
End Get
End Property
End Class
The value 999 is added in the base class just to make it easier to see the difference.
In the base class, the sub useMe just references theProperty. Me is implied in this case and if you use it anyway ...
Console.WriteLine(
"Property using Me: " &
Me.theProperty)
... you get exactly the same result; that is, theProperty in the derived class is referenced. But when the MyClass keyword is used, the property in the base class is displayed.
--------
Click Here to display the illustration
--------
