MyClass behaves like an object variable referring to the current instance of a class as originally implemented. MyClass is similar to Me, but all method calls on it are treated as if the method were NotOverridable. Therefore, the method being called is not affected by overriding in a derived class. The following example compares Me and MyClass.
Class BaseClass
Public Overridable Sub MyMethod()
MsgBox("Base class string")
End Sub
Public Sub UseMe()
Me.MyMethod() ' Use calling class's version, even if an override.
End Sub
Public Sub UseMyClass()
MyClass
.MyMethod() ' Use this version and not any override.
End Sub
End Class
Class DerivedClass : Inherits BaseClass
Public Overrides Sub MyMethod()
MsgBox("Derived class string")
End Sub
End Class
Class TestClasses
Sub StartHere()
Dim TestObj As DerivedClass = New DerivedClass()
TestObj.UseMe() ' Displays "Derived class string".
TestObj.UseMyClass() ' Displays "Base class string".
End Sub
End Class
Even though DerivedClass
overrides MyMethod
, the MyClass keyword in UseMyClass
nullifies the effects of overriding, and the compiler resolves the call to the base class version of MyMethod
.
MyClass cannot not be used inside a Shared method, but you can use it inside an instance method to access a shared member of a class.