shared member and inheritance : how to ?

Hi

I have 3 classes : Base, Child1, Child2

Class MustInherit Base

     Shared Stuff As ...

     Sub DoStuff
         for each x in stuff
            ...
         next
     End Sub

End Class

Class Child1
     Inherits Base

End Class

Class Child2
     Inherits Base

End Class

For now the Child1 and Child2 instances share the same "stuff" parameter. But i want only the Child1 instances to share Child1 "stuff" and the same respectively for Child2 "stuff"

How should I do that

Thanks for your answers



Answer this question

shared member and inheritance : how to ?

  • Mark Chad

    Shared means they all share the same instance.

    what you want is:

    MustInherit Public class TheBase
    Private Stuff as Something

    Public Sub New
    Stuff = New Something
    End Sub

    Protected Sub DoSomething
    For Each item as Object in Stuff
    . . .
    Next
    End Sub
    End Class

    Public Class1
    Inherits TheBase

    Public Sub New()
    MyBase.New()
    End Sub
    End Class

    Public Class2
    Inherits TheBase

    Public Sub New()
    MyBase.New()
    End Sub
    End Class



  • Julius Rastad

    Public MustInherit Class TheBase
        Protected MustOverride ReadOnly Property TheStuff() As Object 
        Public Sub DoSomething()  
            MsgBox(TheStuff.ToString())  
       
    End Sub
    End
    Class
     
    Public
    Class Child1 
        Inherits TheBase 
     
        Shared
    Somestuff As Object
        Protected Overrides ReadOnly Property TheStuff() As Object 
           
    Get  
               
    Return somestuff  
           
    End Get
        End Property
    End
    Class 

    Public
    Class Child2 
        Inherits TheBase 

        Shared
    Somestuff As Object
        Protected Overrides ReadOnly Property TheStuff() As Object 
            Get 
                Return somestuff 
            End Get 
        End Property
    End
    Class


  • ffe_bob

    Thanks for your posts.

    That's approximately how I was doing it. I was just wondering if there was an better way to to that.


  • bw_123

    That's not what I want.

    I want child1 instances, like mychild1_1, mychild1_2, mychild2_3 to share the same "stuff" but not the child2 instances, they will have their own shared "stuff".


  • keenan10000

    You will have to define/redefine the stuff on child1 and child2 separetely - anything defined in the base is accessible to it's derived classes.

  • shared member and inheritance : how to ?