none
[VB]Shared Properties in Base Classes vs Derived Classes RRS feed

  • Question

  • I have a base class with properties that I want to be shared between instances of the derived classes, but not between the different derived classes. Here is my current code:
    Public MustInherit Class MyBaseClass
    	Public Shared Property MySharedProperty() As String
    	Public Sub New(value As String)
    		MySharedProperty = value
    	End Sub
    End Class
    
    Public Class MyDerivedClassOne : Inherits MyBaseClass
    	Public Sub New()
    		MyBase.New("MyDerivedClassOne")
    	End Sub
    End Class
    
    Public Class MyDerivedClassTwo : Inherits MyBaseClass
    	Public Sub New()
    		MyBase.New("MyDerivedClassTwo")
    	End Sub
    End Class

    If I have an instance of MyDerivedClassOne and MyDerivedClassTwo, I want them to (possibly) have different values for MySharedProperty, but if I have two instances of MyDerivedClassOne, I want MySharedProperty to have the same value. I could do the following:
    Public MustInherit Class MyBaseClass
    	Public Sub New()
    	End Sub
    End Class
    
    Public Class MyDerivedClassOne : Inherits MyBaseClass
    	Public Readonly Shared Property MySharedProperty() As String = "MyDerivedClassOne"
    	Public Sub New()
    	End Sub
    End Class
    
    Public Class MyDerivedClassTwo : Inherits MyBaseClass
    	Public Readonly Shared Property MySharedProperty() As String = "MyDerivedClassTwo"
    	Public Sub New()
    	End Sub
    End Class
    However, this would mean that MySharedProperty was not inherited. In my current code (the first code block in this post), is the shared property shared between derived classes of the same type or all derived classes? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, February 25, 2018 9:51 PM

All replies

  •  In my current code (the first code block in this post), is the shared property shared between derived classes of the same type or all derived classes? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Neither.  Shared members belong to the type on which they are declared.  The shared member value may be available on the derived classes but it is being exposed through the parent type.  There will only be one value for MySharedProperty and that value would depend on which derived class was instantiated last.  Surely this is not what you want to do.

    Can you give a concrete example of what you are trying to accomplish?  This abstract example isn't very helpful because the final answer most likely depends on exactly what you are trying to accomplish with the shared properties.  And not just the "what" but the "why" of it was well.  It may be that the current thinking doesn't coincide with object oriented design and needs to be rethought to fit the code model.

    [...]

    -EDIT-

    Sorry, I see now that both threads were moved into this forum and the other one, while somewhat related to this one, is more a question about auto properties.  As long as the two topics stay separated, there is no reason to combine them.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Monday, February 26, 2018 12:34 AM
    Moderator