Select specific properties in derived classes


J

Josh Valino

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
 
Ad

Advertisements

J

Jeroen Mostert

Josh said:
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).
 
E

Eric

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.
 
J

Jeroen Mostert

Eric said:
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 "composition" rather than "aggregation".
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.
 
Ad

Advertisements

J

Josh Valino

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top