views:

136

answers:

2

Hi, now i'm hoping the following is possible although I'm not entirely certain it is so here goes...

Below is the setup of what I'm hoping is possible (in VB.net, feel free to answer in C# and I should be able to work it out):

Public Class A

    Private _name As String
    Private _s As SearchA

    Public Property Name() As String
         Get
            Return _name
         End Get
         Set(ByVal Value As String)
            _name = Value
         End Set
    End Property
    Public Property S() As SearchA
         Get
            Return _s
         End Get
         Set(ByVal Value As SearchA)
            _s = Value
         End Set
    End Property

    Public Sub New(ByVal name As String)
         _name = name 
         _s = New SearchA()
    End Sub

    Public Function GetSearch() As String
         Return _s.Search
    End Sub

End Class

and

Public Class SearchA

    Private _search As String

    Public Property Search () As String
         Get
            Return _search 
         End Get
         Set(ByVal Value As String
            _search = Value
         End Set
    End Property

    Public Sub New()
         _search = "Search using Base"
    End Sub
End Class

and

Public Class B
    Inherits A

    Private Shadows _s As SearchB

    Public Shadows Property S() As SearchB
         Get
            Return _s
         End Get
         Set(ByVal Value As SearchB)
            _s = Value
         End Set
    End Property

    Public Sub New(ByVal name As String)
         Mybase.New(name)
         _s = New SearchB()
    End Sub

End Class

and finally

Public Class SearchB
    Inherits SearchA

    Private _superSearch As String

    Public Property SuperSearch  () As String
         Get
            Return _superSearch 
         End Get
         Set(ByVal Value As String
            _superSearch = Value
         End Set
    End Property

    Public Sub New()
        Mybase.New()
         _search = "Search using New"
         _superSearch = "With more options..."
    End Sub

End Class

and here's the usage:

Dim oB As New B("hello")
Response.Write(oB.GetSearch())

I thought that shadows might work and print "Search using New" but it doesn't, any ideas? I can't override as the property has a different return type to the base class property. I want to define within a base class a core set of functions that I don't have to override within each child class. Or does this not make much sense?

Thanks for your help!

Steve

A: 

I think what you want to do is define an abstract Search class that is inherited by SearchA and SearchB. Make _s a protected Search type, expose it as a public property, and assign SearchA to it in class A and SearchB to it in class B. Then GetSearch() will return SearchA from A and SearchB from B, although they will both be typed as Search.

Jamie Ide
Thanks Jamie, I did think that's the only option which unfortunately in my case renders the principle of what I was trying to do useless.
What are you trying to do? It wasn't clear from the question. If you re-phrase it you might get more/better responses.
Jamie Ide
A: 

I think it is because GetSearch is implemented in A, so it uses the fields from A.

Generally, if you use Shadows not in legacy code, you should ask yourself what you are doing wrong.

As it is here, this design has a smell. Both cases of inheritance B from A and SearchB from SearchA are kind of misusing inheritance. It's kind of inheriting a cat from a dog because it's "mostly the same". As Jamie Ide already said: you should use abstract base classes.

Consider to:

  • use a abstract baseclass and inherit A and B
  • Use a interface ISearch implemented by SearchA and SearchB that is reference by the base class. It's a classical strategy pattern.
  • Use generics if you need a typed references to SearchA or SearchB. You probably don't need it.
  • use factories to put things together
Stefan Steinegger