none
MyBase: does the control move horizontally? RRS feed

  • Question

  • My question is as follows:

    In the below code when the statement MyBase.someSub() gets executed, the compiler does not find the someSub in the base class. Now that is probably the reason why the red squiggly line is coming. My question is does MyBase move only in the vertical direction upwards towards the ultimate base class OR is there a way to make it move horizontally? For example, in this case can we direct the compiler to look into the inherited_1b class (which also inherits the base class) when it does not find someSub in base class

    Module Module1
    
        Class base
            'Nothing
        End Class
    
        Class inherited_1a : Inherits base
            Public Sub anotherSub()
                MyBase.someSub() 'getting error here. red squiggly line
            End Sub
        End Class
    
        Class inherited_1b : Inherits base
            Public Sub someSub()
                Console.WriteLine("From the inherited_1b class")
            End Sub
        End Class
    
        Sub main()
            Dim i As New inherited_1a()
            i.anotherSub()
            Console.ReadLine()
        End Sub
    
    End Module

    The question may sound weird but it is just of curiosity and purely academic in nature. 


    Sougata Ghosh


    • Edited by sougata12 Saturday, October 12, 2019 8:38 PM
    Saturday, October 12, 2019 8:38 PM

Answers

  • Hello,

    The base class can tell who inherited it but what you are after is not possible.

    Consider the change to make things correct along with note that base knows who the owner is.

    Module Module1
    
        Class base
            'Nothing
    
            Public Sub New()
                Console.WriteLine(Me.GetType().Name)
            End Sub
    
            Protected Sub someSub()
                '
            End Sub
        End Class
    
        Class inherited_1a : Inherits base
            Public Sub anotherSub()
                MyBase.someSub() 'getting error here. red squiggly line
    
            End Sub
        End Class
    
        Class inherited_1b : Inherits base
            Public Sub someSub()
                Console.WriteLine("From the inherited_1b class")
            End Sub
        End Class
    
        Sub main()
            Dim i As New inherited_1a()
            i.anotherSub()
            Console.ReadLine()
        End Sub
    
    End Module
    

    Use

    Dim test = New base

    Then use this

    Dim test = New inherited_1a
    On a side note even learning pick better names for classes and variable, never use one character variables.

    See below and also checkout a bonus in Inherited 2

    Module Module1
    
        Class Base
            'Nothing
    
            Public Sub New()
                Console.WriteLine(Me.GetType().Name)
            End Sub
    
            Protected Sub SomeProcedureInBaseClass()
                '
            End Sub
        End Class
    
        Class Inherited1 : Inherits Base
            Public Sub AnotherProcedureInInherited1()
                SomeProcedureInBaseClass() 'getting error here. red squiggly line
            End Sub
        End Class
    
        Class Inherited2 : Inherits Base
            Public Sub SomeProcedureInInherited2(
                <Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing)
                Console.WriteLine($"From the {memberName} in {Reflection.MethodBase.GetCurrentMethod().Name}")
            End Sub
        End Class
    
        Sub Main()
            Dim Inherited1 As New Inherited1()
            Inherited1.AnotherProcedureInInherited1()
            Console.ReadLine()
        End Sub
    
    End Module
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Sunday, October 13, 2019 8:30 AM
    Saturday, October 12, 2019 11:12 PM
    Moderator

All replies

  • Hello,

    The base class can tell who inherited it but what you are after is not possible.

    Consider the change to make things correct along with note that base knows who the owner is.

    Module Module1
    
        Class base
            'Nothing
    
            Public Sub New()
                Console.WriteLine(Me.GetType().Name)
            End Sub
    
            Protected Sub someSub()
                '
            End Sub
        End Class
    
        Class inherited_1a : Inherits base
            Public Sub anotherSub()
                MyBase.someSub() 'getting error here. red squiggly line
    
            End Sub
        End Class
    
        Class inherited_1b : Inherits base
            Public Sub someSub()
                Console.WriteLine("From the inherited_1b class")
            End Sub
        End Class
    
        Sub main()
            Dim i As New inherited_1a()
            i.anotherSub()
            Console.ReadLine()
        End Sub
    
    End Module
    

    Use

    Dim test = New base

    Then use this

    Dim test = New inherited_1a
    On a side note even learning pick better names for classes and variable, never use one character variables.

    See below and also checkout a bonus in Inherited 2

    Module Module1
    
        Class Base
            'Nothing
    
            Public Sub New()
                Console.WriteLine(Me.GetType().Name)
            End Sub
    
            Protected Sub SomeProcedureInBaseClass()
                '
            End Sub
        End Class
    
        Class Inherited1 : Inherits Base
            Public Sub AnotherProcedureInInherited1()
                SomeProcedureInBaseClass() 'getting error here. red squiggly line
            End Sub
        End Class
    
        Class Inherited2 : Inherits Base
            Public Sub SomeProcedureInInherited2(
                <Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing)
                Console.WriteLine($"From the {memberName} in {Reflection.MethodBase.GetCurrentMethod().Name}")
            End Sub
        End Class
    
        Sub Main()
            Dim Inherited1 As New Inherited1()
            Inherited1.AnotherProcedureInInherited1()
            Console.ReadLine()
        End Sub
    
    End Module
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by sougata12 Sunday, October 13, 2019 8:30 AM
    Saturday, October 12, 2019 11:12 PM
    Moderator
  • Thanks for your other suggestions also. Shall keep them in mind

    Sougata Ghosh

    Sunday, October 13, 2019 10:34 AM