O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 to Me) to qualify a method invocation, as in:

    MyClass.IncSalary(  )

    the method is treated as if it was declared using the NotOverridable keyword. 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 between MyClass and Me.

  • MyClass cannot 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 Class

Now 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, 11000

The first call to ShowIncSalary is made using a variable of type Class1 that refers to an object of type ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required