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

Me Operator

Syntax

Me

Description

Represents a reference to the current class from within the class

Rules at a Glance

  • Me is an explicit reference to the current object as defined by the Class...End Class construct.

  • Me corresponds to the C++ this operator.

Example

In this example, a class passes an instance of itself to a function outside the class by using the Me operator.

Private Class CCounter

Private lCtr As Long = 1

Public ReadOnly Property Value
   Get
      Value = lCtr
   End Get
End Property

Public Sub Increment(  )
   lCtr += 1
End Sub

Public Function ShowCount(  ) As Long
   ShowCount = ShowObjectValue(Me)
End Function

End Class

Module modMain

Public Sub Main
   Dim oCtr = New CCounter
   oCtr.Increment
   oCtr.Increment
   MsgBox("Count: " & oCtr.ShowCount)
End Sub

Public Function ShowObjectValue(oObj As Object) AS Object
   ShowObjectValue = oObj.Value
End Function

End Module

Programming Tips and Gotchas

  • The Me operator can’t be used on the left side of an expression.

  • Me is particularly useful when passing an instance of the current class as a parameter to a routine outside the class.

See Also

MyClass Keyword

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