Results 1 to 6 of 6

Thread: [RESOLVED] Inherited class needs to return a must override property of a different type

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Resolved [RESOLVED] Inherited class needs to return a must override property of a different type

    Hi all,

    I have a mustInherit class (baseClass) and a MustInherit class (baseSubClass) and the base class has a MustOverride Property List (of baseSubClass)
    Code:
    Public MustInherit Class baseClass
        Public MustOverride Property mySubClasses As List(Of baseSubClass)
    End Class
    Public MustInherit Class baseSubClass
    End Class
    I need to inherit these classes and have the mySubClasses property in the inherited base class return a list of the inherited sub class.
    Code:
    Public Class A
        Inherits baseClass
        Private _mySubClasses As List(Of subClassA)
        Public Overrides Property mySubClasses() As List(Of subClassA)
            Get
                Return _mySubClasses
            End Get
            Set(ByVal value As List(Of subClassA))
                _mySubClasses = value
            End Set
        End Property
    
    End Class
    Public Class subClassA
        Inherits baseSubClass
    
    End Class
    This does not work though because the mySubClasses property in the inherited class barks with this...

    Public Overrides Property mySubClasses As System.Collections.Generic.List(Of subClassA)' cannot override 'Public MustOverride Property mySubClasses As System.Collections.Generic.List(Of baseSubClass)' because they differ by their return types.
    What is the best way to handle a situation like this?


    In case you are wondering, the inherited baseClass represent is a specific manufacture/model of a PLC and the sub class represents the actual IO points on the PLC. Because different plc manufactures have different IO point properties, I need to create a class for each plc and associated points.
    thanks
    Kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,044

    Re: Inherited class needs to return a must override property of a different type

    I don't understand the situation well enough to know whether an ideal solution is possible for this. The ideal solution would be that your return a list of the base class and don't worry about it, because all the properties and methods in the subclass are overriden properties or methods of the base class. It may be that property A in subclass type T makes no sense, whereas the same property in subclass type P does make sense, so depending on the return type, T.A could return Nothing, 0, -1, or whatever indicator makes sense, whereas P.A returns an actual value.

    Such a case would be easiest if the meaningful information in the subclasses overlapped greatly. It would also be possible to add a further property that expose information about what was and was not available for type T and P, such that any code using the class could have a common point of reference to know whether A made sense for the actual object in question. So, all you'd have is a bunch of base class objects, and you could seek all the items for which the A property was valid, or something like that.

    Another alternative is that the IO points were objects themselves, such that the PLC, regardless of what set of points it had, exposes a list of points, which themselves had different properties depending on what made sense for them.

    EDIT: I guess I'd go further and generalize the suggestion in this way: The best solution is if you can work with all the subclasses cast to base classes and let runtime polymorphism handle the differences. If that isn't possible, then the only advantage that the base class confers on the derived classes is some common functionality, which could have just as easily been copied into all the subclasses. So, the base class is there for efficiency in maintaining the code, but doesn't really assist in any other way. If that is the case, then rather than focusing on the parent child relationship, you might consider creating some factory class that created instances of the sub classes, and possibly even held lists of the sub classes, and they are never cast to their base for any reason because there is no good reason to ever use them as base class instances.
    Last edited by Shaggy Hiker; Feb 24th, 2015 at 12:50 PM.
    My usual boring signature: Nothing

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Inherited class needs to return a must override property of a different type

    Generics?
    Code:
    Public MustInherit Class baseClass(Of T As baseSubClass)
        Public MustOverride Property mySubClasses() As List(Of T)
    End Class
    Public MustInherit Class baseSubClass
    End Class
    
    
    Public Class A
        Inherits baseClass(Of subClassA)
        Private _mySubClasses As List(Of subClassA)
       Public Overrides Property mySubClasses() As List(Of subClassA)
          Get
             Return _mySubClasses
          End Get
          Set(ByVal value As System.Collections.Generic.List(Of subClassA))
             _mySubClasses = value
          End Set
       End Property
    End Class
    Public Class subClassA
        Inherits baseSubClass
    End Class
    Side note: Shouldn't mySubClasses be Readonly?

  4. #4

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: Inherited class needs to return a must override property of a different type

    @Shaggy,
    I had to read that 3 times to digest it, but it made me look at my classes and realize that I CAN simply return the base types and be done with it. Then I read this...
    Side note: Shouldn't mySubClasses be Readonly?
    and I thought, now how would he know that? But he's right. The base class (PLC) is responsible for creating it sub classes (IO points.) Also, in the real world as I look at 2 different PLC's next to me, both have IO points that to me look and behave the same on either plc. I can plug the same sensor into either plc and I get the same voltage and digital state from either as well when I measure it. So this leads to the fact that only the PLC needs to have a particular subClass instance knowlegde, but looking at the PLC from the outside, all I can see are base IO points. This leads to a simple implementation of providing a method to return a list(of baseSubClass) to the outside world.

    @TnT
    I've never worked with generics before, and since your code compiles I feel compelled to learn about them and junk up my brain with more stuff.
    thanks guys
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: [RESOLVED] Inherited class needs to return a must override property of a differen

    Kevin,

    The reason that I mentioned ReadOnly is that when you expose a collection as a Property is that often the collection itself is created in the class and you only want to expose its Add/Remove methods and not allow another collection to be assigned to it.

    To expose a collection and prevent any changes to the collection, you can expose it as a System.Collections.ObjectModel.ReadOnlyCollection.

    Code:
    Public ReadOnly Property mySubClasses() As System.Collections.ObjectModel.ReadOnlyCollection(Of subClassA) Implements Ibc(Of subClassA).mySubClasses
       Get
          Return New System.Collections.ObjectModel.ReadOnlyCollection(Of subClassA)(_mySubClasses)
       End Get
    End Property
    Edit: This is not to say that changes to the individual items can not be made; it is just that you can not assign a new instance to the item.
    Last edited by TnTinMN; Feb 24th, 2015 at 02:13 PM.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    5,683

    Re: [RESOLVED] Inherited class needs to return a must override property of a differen

    For completeness sake, I will mention that you can shadow members of a base class. You can use a different return type this way.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width