? difference between: Me, MyBase and Parent

I don t know what the difference is between these three items: me, Mybase, parent

When do we use each of them.

Also is the quivalent of this in C#: Me or MyBase

Gracias Amigos:)




Answer this question

? difference between: Me, MyBase and Parent

  • Jeff Micro

    Me is a reference to the current object. Often optional to use, but helps clarify your code when accessing methods and properties with objects with similar names.

    MyBase is a reference to the base object: the current object may have inherited from a base object. Used when you explicitly want to access a property or method of the base class.

    Parent is the 'owner' or container of a control or form (Graphical controls) and is not relevant to a 'plain' vanilla class or object.



  • rlieving

    MyBase and MyClass are used mostly to work around virtual calls - the typical use of mybase is to call the base implementation before doing what you need to do for your class:

     

    Module Module1
    
      Sub Main()
        Dim c As New c1
        Console.WriteLine(c.FormalMeetMe())
        Console.WriteLine(c.InformalMeetMe())
      End Sub
    
    End Module
    
    Class c0
      Protected Greeting As String
    
      Overridable Sub foo()
        Greeting = "Hello. I'm class c0."
      End Sub
    
      Function FormalMeetMe() As String
        MyClass.foo()
        Return Greeting
      End Function
    
      Function InformalMeetMe() As String
        foo()
        Return Greeting
      End Function
    End Class
    Class c1
      Inherits c0
    
      Overrides Sub foo()
        MyBase.foo()
        Greeting &= " But some people call me c1."
      End Sub
    End Class
    
    
    Just to add something - myclass and mybase cannot be used as object references - they are provided solely to access members that aren't accessible through shadowing or because through overriding other member would be called instead. You cannot assign myclass or mybase to a variable, for example.


  • JeffWoodard

    thnks, how about the difference between Me, Parent and Mybase

    what is the purpose of each of them

    Thnks



  • Vishal Verma

    The MyBase keyword behaves like an object variable referring to the base class of the current instance of a class.

    The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing. Me behaves like either an object variable or a structure variable referring to the current instance


  • Sebastien

    thanks a lot for the explanantions.

  • Jamie Cool

    In the following example - you will see that this case the base class method foo and returns base.

    Now comment the Foo function in derived with the line return "derived" which will shadow the base class functionality and running the same client code in Main will result in "Derived" being shown. But what would happen if I really wanted to call a base method foo in the derived called. I would need a way to refer to whatever the base class method was and this is why you would use mybase.

    Now comment out the line Return "Derived" and comment in the line Return mybase.foo Then you can specify mybase.foo which would ultimately call the foo method in the base class from the derived. Resulting in "Base" being returned.

    'CLIENT
    Module Module1
    Sub Main()
    Dim x As New derived
    Console.WriteLine(x.Foo)
    End Sub
    End Module

    'CLASSES
    Class base
    Function foo() As String
    Return "base"
    End Function
    End Class

    Class derived
    Inherits base

    'Function Foo() As String
    ' Return "Derived"
    ' Return MyBase.foo
    'End Function

    End Class


  • CraigSBoyd

    In C#, Me is equal to this

    which means pointing to current instance



  • Rick112

    I see, so Mybase is mostly useful if we have an overridden method or property. Otherwise we can just acces the method or the property from the derived class itself rather than from the base class. Is that correct

  • bmkiss67

    MyBase seems to be the most important then. Correct me if i m wrong. We need MyBase in this scenario:

    Public Class NewClass

    Inherits BaseClass

    End class

    then inside the new defined class we can access the methods and properties of the baseclass

    My question is: can't we access those methods and properties within the NewClass just directly without refering to MyBase or will i get an error

    Thanks



  • ? difference between: Me, MyBase and Parent