Dela via


MyClass

Provides a way to refer to the current class instance members without them being replaced by any derived class overrides.

Remarks

The MyClass keyword behaves like an object variable referring to the current instance of a class as originally implemented. MyClass is similar to Me, but every method and property call on MyClass is treated as if the method or property were NotOverridable. Therefore, the method or property is not affected by overriding in a derived class. The following example compares Me and MyClass.

Class baseClass
    Public Overridable Sub testMethod()
        MsgBox("Base class string")
    End Sub
    Public Sub useMe()
        ' The following call uses the calling class's version, even if 
        ' that version is an override.
        Me.testMethod()
    End Sub
    Public Sub useMyClass()
        ' The following call uses this version and not any override.
        MyClass.testMethod()
    End Sub
End Class
Class derivedClass : Inherits baseClass
    Public Overrides Sub testMethod()
        MsgBox("Derived class string")
    End Sub
End Class
Class testClasses
    Sub startHere()
        Dim testObj As derivedClass = New derivedClass()
        ' The following call displays "Derived class string".
        testObj.useMe()
        ' The following call displays "Base class string".
        testObj.useMyClass()
    End Sub
End Class

Even though derivedClass overrides testMethod, the MyClass keyword in useMyClass nullifies the effects of overriding, and the compiler resolves the call to the base class version of testMethod.

You cannot use MyClass inside a Shared (Visual Basic) method, but you can use it inside an instance method to access a shared member of a class.

See Also

Concepts

Inheritance Basics

Reference

Me

MyBase