Living .NET...

Musings on .NET, and the like - Manoj G [MVP, Connected Systems Developer]

Me, MyBase and MyClass...

Me

Me is what really symbolizes VB.NET isn't it? Where is it really applicable ? Lets see:

  • To pass the current executing instance to some other Class or Structure method as a parameter. For example, consider this Print method.
Sub Print()
    Console.WriteLine(
Me)
End Sub
  • I have seen samples where Me is prefixed for every member access. I dont think that is really necessary. But sometimes, this can be used differentiate between a method parameter and an instance member. For example:
Public Sub SetValue(ByVal Value as Int16)
   
Me.Value = Value
End Sub

I would choose to prefix all instance members with an _, especially, if they are private. Like “_value“.

MyBase

MyBase is a keyword used to call the methods of immediate base class from a method in a derived class. Typically, this would be called in overloaded Constructors and the Finalizer. For example:

Sub New(Byval as Int16)
  
MyBase.New(i)

'Do specific initialization
End Sub

Protected Overrides Sub 
Finalize()
    
MyBase.Finalize()
    
' Do Specific cleanup 

End Sub

MyClass

We all would be comfortable with the semantics of Me and MyBase. But MyClass is somewhat tricky. The MSDN definition reads: “MyClass behaves like an object variable referring to the current instance of a class as originally implemented. MyClass is similar to Me, but all method calls on it are treated as if the method were NotOverridable. Therefore, the method being called is not affected by overriding in a derived class“. What does this mean?

There may be situations where a base class method calls another method in the same class which may be declared as Overridable (a virtual method). So, the executing code cannot make the assumption that the current instance is of the base type. So, a method call in the base may actually be dispatched to a derived class instance (if overridden). Therefore, the logic in the calling method may fail if assumptions are made on the type of the instance.

MyClass addresses this situation by making sure that the current class's method is dispatched and not the overridden method. Have a look at this MSDN sample (which is actually pretty clear)

Class BaseClass
   
Public Overridable Sub MyMethod()
      
MsgBox("Base class string")
   
End Sub
   Public Sub 
UseMe()
      
Me.MyMethod()   ' Use calling class's version, even if an override.
   
End Sub
   Public Sub 
UseMyClass()
      
MyClass.MyMethod()   ' Use this version and not any override.
   
End Sub
End Class

Class 
DerivedClass : Inherits BaseClass
   
Public Overrides Sub MyMethod()
      
MsgBox("Derived class string")
   
End Sub
End Class

Class 
TestClasses
   
Sub StartHere()
      
Dim TestObj As DerivedClass = New DerivedClass()
      TestObj.UseMe()   
' Displays "Derived class string".
      
TestObj.UseMyClass()   ' Displays "Base class string".
   
End Sub
End Class

There are some limitations of using these keywords and the link given below explains these:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconInheritanceBasics.asp

Another interesting thing to note is that there is no equivalent of MyClass in C#. It is debatable which compiler implementation is right or wrong. But I prefer the way it works in C# because the behavior of the code is more predictable.

Posted: Jan 29 2004, 11:26 AM by Manoj G | with 4 comment(s)
Filed under:

Comments

Manoj G said:

Nice aricle for the beginers.
# November 30, 2004 6:32 PM

saptarshi dutta said:

Good article...
Keep it up..

Saptarshi dutta
Kalyani
# March 6, 2006 3:44 AM

VIRAJ said:

It's a really nice explanation to the point. Thnks in advance

Regards

VIRAJ

# July 26, 2007 2:43 AM

Wei said:

Nice to read that..  I was confused about mybase and myclass.

# October 31, 2007 9:56 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)