Name
MyClass Keyword
Syntax
MyClass
Description
MyClass is a reference to the class in which the
keyword is used.
Rules at a Glance
When using
MyClass(as opposed toMe) to qualify a method invocation, as in:MyClass.IncSalary( )
the method is treated as if it was declared using the
NotOverridablekeyword. Thus, regardless of the type of the object at runtime, the method called is the one declared in the class containing this statement (and not in any derived classes). The upcoming example illustrates this difference betweenMyClassandMe.MyClasscannot be used with shared members.
Example
The following code defines a class, Class1, and a derived class, Class1Derived, each of which has an IncSalary method.
Public Class Class1
Public Overridable Function IncSalary(ByVal sSalary As Single) _
As Single
IncSalary = sSalary * CSng(1.1)
End Function
Public Sub ShowIncSalary(ByVal sSalary As Single)
MsgBox(Me.IncSalary(sSalary))
MsgBox(MyClass.IncSalary(sSalary))
End Sub
End Class
Public Class Class1Derived
Inherits Class1
Public Overrides Function IncSalary(ByVal sSalary As Single) As Single
IncSalary = sSalary * CSng(1.2)
End Function
End ClassNow consider the following code, placed in a form module:
Dim c1 As New Class1( )
Dim c2 As New Class1Derived( )
Dim c1var As Class1
c1var = c1
c1var.ShowIncSalary(10000) ' Shows 11000, 11000
c1var = c2
c1var.ShowIncSalary(10000) ' Shows 12000, 11000The first call to ShowIncSalary is made using a variable of type Class1 that refers to an object of type ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access