Select specific properties in derived classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josh Valino

    Select specific properties in derived classes

    Hi group,

    Is there a way in C# (.Net 3.5 FW) for me to define an abstract class that
    has some properties, and then in each derived class, select which properties
    I'd like available and which ones I would not? I know that I can put the
    properties that will be available in ALL derived classes in the base class
    and then implement the others in the derived classes, but many of the
    derived classes will share these properties. To illustrate:


    abstract class SomeBaseClass
    {
    //the getters and setters will have actual code in them
    //not just the auto-private member value holders
    public someType property1 { get; set; }
    public someType property2 { get; set; }
    public someType property3 { get; set; }
    public someType property4 { get; set; }
    public someType property5 { get; set; }
    public someType property6 { get; set; }
    }

    class derivedClass1 : SomeBaseClass
    {
    //"enable" property1, property2, property6
    }

    class derivedClass2 : SomeBaseClass
    {
    //"enable" property1, property4, property6
    }

    class derivedClass3 : SomeBaseClass
    {
    //"enable" property2, property6
    }

    class derivedClass4 : SomeBaseClass
    {
    //"enable" property1, property5, property6
    }

    class derivedClass5 : SomeBaseClass
    {
    //"enable" property1, property2, property4, property5
    }


    This is for a report generation tool that allows for content items to be
    added, many of which will share common properties. I do not need any
    additional logic for these properties in the derived classes. My goal is to
    write the property code just in one place but not to then create a derived
    class that has properties that are not applicable, eg. FontStyle for a
    derived class that is used for adding a binary image to the report. It
    wouldn't need that property, but it would need some of the other ones
    pertaining to alignment, etc. I could override the irrelevant properties
    and make them do nothing, but I'm hoping not to display such properties at
    all to the developers in India who will be relying on what shows up in
    Intellisense.

    Thanks

    JV




  • Jeroen Mostert

    #2
    Re: Select specific properties in derived classes

    Josh Valino wrote:
    Is there a way in C# (.Net 3.5 FW) for me to define an abstract class that
    has some properties, and then in each derived class, select which properties
    I'd like available and which ones I would not?
    No. The closest thing C# has to selectively exposing members is explicit
    interface implementation, which doesn't apply here.
    I know that I can put the properties that will be available in ALL
    derived classes in the base class and then implement the others in the
    derived classes, but many of the derived classes will share these
    properties.
    To illustrate:
    >
    >
    abstract class SomeBaseClass
    {
    //the getters and setters will have actual code in them
    //not just the auto-private member value holders
    public someType property1 { get; set; }
    public someType property2 { get; set; }
    public someType property3 { get; set; }
    public someType property4 { get; set; }
    public someType property5 { get; set; }
    public someType property6 { get; set; }
    }
    >
    class derivedClass1 : SomeBaseClass
    {
    //"enable" property1, property2, property6
    }
    >
    class derivedClass2 : SomeBaseClass
    {
    //"enable" property1, property4, property6
    }
    >
    class derivedClass3 : SomeBaseClass
    {
    //"enable" property2, property6
    }
    >
    class derivedClass4 : SomeBaseClass
    {
    //"enable" property1, property5, property6
    }
    >
    class derivedClass5 : SomeBaseClass
    {
    //"enable" property1, property2, property4, property5
    }
    >
    >
    This is for a report generation tool that allows for content items to be
    added, many of which will share common properties. I do not need any
    additional logic for these properties in the derived classes. My goal is to
    write the property code just in one place but not to then create a derived
    class that has properties that are not applicable, eg. FontStyle for a
    derived class that is used for adding a binary image to the report. It
    wouldn't need that property, but it would need some of the other ones
    pertaining to alignment, etc. I could override the irrelevant properties
    and make them do nothing, but I'm hoping not to display such properties at
    all to the developers in India who will be relying on what shows up in
    Intellisense.
    >
    - You could create base classes for the combinations of shared properties,
    if there aren't too many.

    - Instead of deriving from SomeBaseClass, you could aggregate it. In that
    way you could expose the properties as you saw fit (this would still mean
    some redundant code, but you could share any getter/setter logic).

    --
    J.

    Comment

    • Eric

      #3
      Re: Select specific properties in derived classes

      Is aggregate another way to say use an Adapter pattern? If all you want is
      to control what is seen in intellisense just have a thin wrapper around a
      concrete class, and hide the concrete class.

      "Jeroen Mostert" <jmostert@xs4al l.nlwrote in message
      news:4877aa51$0 $14348$e4fe514c @news.xs4all.nl ...
      Josh Valino wrote:
      >Is there a way in C# (.Net 3.5 FW) for me to define an abstract class
      >that has some properties, and then in each derived class, select which
      >properties I'd like available and which ones I would not?
      >
      No. The closest thing C# has to selectively exposing members is explicit
      interface implementation, which doesn't apply here.
      >
      >I know that I can put the properties that will be available in ALL
      >derived classes in the base class and then implement the others in the
      >derived classes, but many of the derived classes will share these
      >properties.
      >To illustrate:
      >>
      >>
      >abstract class SomeBaseClass
      >{
      > //the getters and setters will have actual code in them
      > //not just the auto-private member value holders
      > public someType property1 { get; set; }
      > public someType property2 { get; set; }
      > public someType property3 { get; set; }
      > public someType property4 { get; set; }
      > public someType property5 { get; set; }
      > public someType property6 { get; set; }
      >}
      >>
      >class derivedClass1 : SomeBaseClass
      >{
      > //"enable" property1, property2, property6
      >}
      >>
      >class derivedClass2 : SomeBaseClass
      >{
      > //"enable" property1, property4, property6
      >}
      >>
      >class derivedClass3 : SomeBaseClass
      >{
      > //"enable" property2, property6
      >}
      >>
      >class derivedClass4 : SomeBaseClass
      >{
      > //"enable" property1, property5, property6
      >}
      >>
      >class derivedClass5 : SomeBaseClass
      >{
      > //"enable" property1, property2, property4, property5
      >}
      >>
      >>
      >This is for a report generation tool that allows for content items to be
      >added, many of which will share common properties. I do not need any
      >additional logic for these properties in the derived classes. My goal is
      >to write the property code just in one place but not to then create a
      >derived class that has properties that are not applicable, eg. FontStyle
      >for a derived class that is used for adding a binary image to the report.
      >It wouldn't need that property, but it would need some of the other ones
      >pertaining to alignment, etc. I could override the irrelevant properties
      >and make them do nothing, but I'm hoping not to display such properties
      >at all to the developers in India who will be relying on what shows up in
      >Intellisense .
      >>
      - You could create base classes for the combinations of shared properties,
      if there aren't too many.
      >
      - Instead of deriving from SomeBaseClass, you could aggregate it. In that
      way you could expose the properties as you saw fit (this would still mean
      some redundant code, but you could share any getter/setter logic).
      >
      --
      J.

      Comment

      • Jeroen Mostert

        #4
        Re: Select specific properties in derived classes

        Eric wrote:
        Is aggregate another way to say use an Adapter pattern?
        No. Nothing's being adapted. Reviewing the terminology, though, it seems I
        should have said "compositio n" rather than "aggregatio n".
        If all you want is to control what is seen in intellisense just have a
        thin wrapper around a concrete class, and hide the concrete class.
        >
        This is what I mean too, except I don't see it as an application of the
        Adapter pattern, since the original class would presumably be wholly
        internal and not exposed to clients. It would be implementation reuse.

        --
        J.

        Comment

        • Josh Valino

          #5
          Re: Select specific properties in derived classes

          What I wound up doing was creating protected properties and then just
          creating corresponding public properties that were relevant in the derived
          classes that just acted as pass-throughs to the underlying protected
          properties in the base class. Of course, then it turned out that I didn't
          really need any logic in the getters or setters when some requirements
          changed, so I undid it and just used plain old public properties in the
          derived classes in the end. :)

          "Jeroen Mostert" <jmostert@xs4al l.nlwrote in message
          news:4877aa51$0 $14348$e4fe514c @news.xs4all.nl ...
          Josh Valino wrote:
          >Is there a way in C# (.Net 3.5 FW) for me to define an abstract class
          >that has some properties, and then in each derived class, select which
          >properties I'd like available and which ones I would not?
          >
          No. The closest thing C# has to selectively exposing members is explicit
          interface implementation, which doesn't apply here.

          Comment

          Working...