1

Possible Duplicate:
Shadows vs. Overrides in VB.Net

What's the difference between shadowing a function in a base class in a subclass and overriding the same function? There is performance issues involved too?

I know how to shadow and how to override in VB.net. My question is about when and why should I shadow a function instead override it and vice-versa.

Thanks!

Community
  • 1
  • 1
Alex
  • 3,325
  • 11
  • 52
  • 80

2 Answers2

1

Here is an answer in terms of C#, but I believe that the top explanations are easily understood in terms of VB.Net.

Shadowing in VB.Net is done with "new" in C#, and overriding explicitly uses "override". Let me know if anything else is difficult to understand from the VB.Net point of view.

Community
  • 1
  • 1
Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
  • Thanks for answering, but I know how to shadow and how to override in VB.net. My question is about when and why should I shadow a function instead override it and vice-versa. – Alex Apr 04 '11 at 19:58
1

I haven't found a use for Shadows yet. One difference is that if the method you are overriding / shadowing is called from the base class, if its overwritten the method in the sub class will be called, if its shadowed the base method will still be called (only the sub class knows about the shadowed output).

See the example code, it outputs A,B A

   Sub Main()

        Dim ClassA As New A
        Dim ClassB As New B
        Dim ClassC As New C

        Console.WriteLine(ClassA.GetText)
        Console.WriteLine(ClassB.GetText)
        Console.WriteLine(ClassC.GetText)

        Console.ReadKey()
    End Sub


    Public Class A

        Public Function GetText()
            Return GetSomeText()
        End Function

        Protected Overridable Function GetSomeText()
            Return "A"
        End Function

    End Class

    Public Class B
        Inherits A

        Protected Overrides Function GetSomeText() As Object
            Return "B"
        End Function

    End Class

    Public Class C
        Inherits A

        Protected Shadows Function GetSomeText()
            Return "C"
        End Function
    End Class
Cwoo
  • 355
  • 4
  • 12