MyClass Keyword

   
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 Class1. In this case, both calls:

 Me.ShowIncSalary MyClass.ShowIncSalary 

return the same value, because they both call IncSalary in the base class Class1.

However, in the second case, the variable of type Class1 holds a reference to an object of the derived class Class1Derived. In this case, Me refers to an object of type Class1Derived, whereas MyClass still refers to the base class Class1 wherein the keyword MyClass appears. Thus:

 Me.ShowIncSalary 

returns 12000, whereas:

 MyClass.ShowIncSalary 

returns 10000.

VB.NET/VB 6 Differences

The MyBase keyword is new to VB.NET.

See Also

Me Operator

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net