none
Can someone please explain this MyBase statement? RRS feed

  • Question

  • The method that MyBase qualifies does not have to be defined in the immediate base class; it may instead be defined in an indirectly inherited base class. In order for a reference qualified by MyBase to compile correctly, some base class must contain a method matching the name and types of parameters that appear in the call.

    Can someone please explain the above statement with an example? Found this statement in the MSDN documentation of MyBASE.

    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/inheritance-basics


    Sougata Ghosh



    Wednesday, September 4, 2019 6:43 PM

Answers

  • I think that it means that MyBase represents “my bases” or “my ancestors”. Example:

        Class Class1

            Public Sub SomeSub()

                ' . . .

            End Sub

        End Class

     

        Class Class2

            Inherits Class1

     

        End Class

     

        Class Class3

            Inherits Class2

     

            Public Sub Test()

                MyBase.SomeSub()

            End Sub

        End Class

     

    Using ‘MyBase.SomeSub( )’ in Class3 you can access SomeSub from Class1, even if Class1 is not the immediate base of Class3.

    • Marked as answer by sougata12 Thursday, September 5, 2019 3:34 AM
    Wednesday, September 4, 2019 8:19 PM
  • Hi,

    The examples given above are very detailed, so I will just add them.
    MyBase refers to the parent class object in the inheritance class, which is used to call the method of the base class of the class. Like the example above, it can be inherited directly or indirectly. For example, Mybase. New, the constructor of Form1, is to call the New method of Windows. Form, the base class of Form, to generate a new form. In addition, if the base class method has parameters, for example, Sub Print (ByVal s As String, ByVal n As Integer). When you call base class methods in inheritance classes, you must match the number and type of parameters, such as use MyBase. Print ("123", 3).

    Hope I can help you.

    Best regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, September 5, 2019 5:13 AM
    Moderator

All replies

  • I think that it means that MyBase represents “my bases” or “my ancestors”. Example:

        Class Class1

            Public Sub SomeSub()

                ' . . .

            End Sub

        End Class

     

        Class Class2

            Inherits Class1

     

        End Class

     

        Class Class3

            Inherits Class2

     

            Public Sub Test()

                MyBase.SomeSub()

            End Sub

        End Class

     

    Using ‘MyBase.SomeSub( )’ in Class3 you can access SomeSub from Class1, even if Class1 is not the immediate base of Class3.

    • Marked as answer by sougata12 Thursday, September 5, 2019 3:34 AM
    Wednesday, September 4, 2019 8:19 PM
  • Thank you

    Sougata Ghosh

    Thursday, September 5, 2019 3:34 AM
  • Hi,

    The examples given above are very detailed, so I will just add them.
    MyBase refers to the parent class object in the inheritance class, which is used to call the method of the base class of the class. Like the example above, it can be inherited directly or indirectly. For example, Mybase. New, the constructor of Form1, is to call the New method of Windows. Form, the base class of Form, to generate a new form. In addition, if the base class method has parameters, for example, Sub Print (ByVal s As String, ByVal n As Integer). When you call base class methods in inheritance classes, you must match the number and type of parameters, such as use MyBase. Print ("123", 3).

    Hope I can help you.

    Best regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, September 5, 2019 5:13 AM
    Moderator
  • Note an additional aspect: you do not have to always specify MyBase. The above sample works if you simply write SomeSub( ). But if Class3 contains another ‘Sub SomeSub( )’, then, in order to differentiate these two homonymous routines, you can write MyBase.SomeSub to access Class1, or SomeSub( ) or Me.SomeSub( ) to access Class3.

    Thursday, September 5, 2019 5:25 AM
  • thanks for the additional insight @Viorel

    Sougata Ghosh

    Friday, September 6, 2019 12:29 PM