SyntaxMyClass DescriptionThe MyClass keyword provides a reference to the class in which the keyword is used. Usage at a Glance
ExampleThe following code defines a base class and a derived class, each of which has an IncSalary method. Public Class BaseClass Public Overridable Function SuperSize _ (ByVal startValue As Long) As Long ' ----- Double it! Return startValue * 2 End Function Public Sub ShowSuperSize(ByVal startValue As Long) MsgBox(Me.SuperSize(startValue)) MsgBox(MyClass.SuperSize(startValue)) End Sub End Class Public Class DerivedClass Inherits BaseClass Public Overrides Function SuperSize _ (ByVal startValue As Long) As Long ' ----- Triple it! Return startValue * 3 End Function End Class Consider the following code, placed in a form module: Dim testWithBase As New BaseClass( ) Dim testWithDerived As New DerivedClass( ) Dim pointToTest As BaseClass pointToTest = testWithBase pointToTest.ShowSuperSize(100) ' Shows 200, 200 pointToTest = testWithDerived pointToTest.ShowSuperSize(100) ' Shows 300, 200 The first call to ShowSuperSize is made using a variable of type BaseClass that refers to an object of type BaseClass. In this case, both calls: Me.ShowSuperSize MyClass.ShowSuperSize return the same value, because they both call BaseClass.SuperSize. However, in the second case, the variable of type BaseClass holds a reference to an object of type DerivedClass. In this case, Me refers to an object of type DerivedClass, whereas MyClass still refers to BaseClass. So the statement: Me.ShowSuperSize(100) returns 300, while: MyClass.ShowSuperSize(100) returns 200. Version DifferencesThe My Class keyword is new to VB under .NET. See AlsoMe Keyword, MyBase Keyword |