none
Accessing base class element from derived class RRS feed

  • Question

  • I was trying to see different ways i can access base class elements from the derived class. The first code that I wrote is shown below. It may be noted that the name Temp has been used multiple times: "Temp" is a variable in the base class, Temp() is a subroutine in the derived class. In this case I am getting an error in the statement Console.WriteLine("Through Sub TEMP() in Class TWO: var = {0}", temp). In other words, I am not able to access the variable temp in the base class from the derived class.

    Module Module1
        Sub Main()
            Dim instance_two As New two()
            instance_two.Temp()
            Console.ReadLine()
        End Sub
    End Module
    
    Class one
        Public temp As Integer = 42
        'Trying to access this 
    End Class
    
    Class two : Inherits one
        Shadows Sub Temp()
            Console.WriteLine("Through Sub TEMP() in Class TWO: var = {0}", temp)
            'Error comes in above line under Temp.        
        End Sub
    End Class

    However when we change the name of the subroutine temp() in the derived class to something else, we are able to access the variable temp in the base class using the console.writeline statement in the derived class. See below:

    Module Module1
        Sub Main()
            Dim instance_two As New two()
            instance_two.Test() 'instance_two.Temp() changed to instance_two.Test()
            Console.ReadLine()
        End Sub
    End Module
    Class one
        Public temp As Integer = 42
    End Class
    Class two : Inherits one
        Sub Test() 'Temp() changed to Test()
            Console.WriteLine("Through Sub TEMP() in Class TWO: var = {0}", temp)
            'Now the above statement works fine. Output is 42 as desired
        End Sub
    End Class

    Can someone please explain what is happening? Why is the compiler not able to access the variable temp = 42 in the first case and why things work fine when the name of the subroutine is changed?


    Sougata Ghosh

    Friday, May 15, 2020 10:26 AM

All replies

  • Hi sougata12,

    Thank you for posting here.

    The method name of a derived class should not be the same as the property name of the base class.

    If you define a variable in a base class:

    Class two : Inherits one
        Public temp As Integer = 42
        Sub Temp()
            Console.WriteLine("Through Sub TEMP() in Class TWO: var = {0}", temp)
        End Sub
    End Class

    You will get following error:

    Member names (field, property, and method) must be unique, because it would cause confusion if the names could be the same. There are some cases where ambiguity could result - for example, when using Action delegates and so on, where methods do not need to have parenthesis, and when using var.

    Best Regards,

    Xingyu Zhao


    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, May 28, 2020 7:43 AM
    Moderator