By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,468 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,468 IT Pros & Developers. It's quick & easy.

Select specific properties in derived classes

P: n/a
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


Jul 11 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
Jul 11 '08 #2

P: n/a
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" <jm******@xs4all.nlwrote in message
news:48***********************@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.

Jul 12 '08 #3

P: n/a
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 "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.

--
J.
Jul 12 '08 #4

P: n/a
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" <jm******@xs4all.nlwrote in message
news:48***********************@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.

Jul 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.