Class structure question - how to inherit properly?

K

K Viltersten

Suppose there is the following class structure.
class S {...}
class A : S {...}
class B : S {...}
class A1 : A {void m(){...}}
class B1 : B {void m(){...}}
class B2 : B {...}

Just to clarify the issue. A1 and B1 share a
method (void m() ) AND B2 doesn't have that
method. Hence, it shouldn't be placed in any
of the extended classes S, A nor B.

Still, i feel that i'd like to put the method
in a common repository/class/whatever, to
avoid repetition and make the changes to it
simpler (only one copy needs to be edited).
It also makes the code less prone to errors.

Can it be done in C#? What would be a smart
solution for it?
 
K

K Viltersten

Suppose there is the following class structure.
These are just a few of the questions that come to mind. My implication
being that we really don't have enough information to answer the
question. At the very simplest, the obvious answer to your question is to
have a utility class with the implementation of your method in it, and
which is called by the method in each class. The extension method
suggestion is just a short hop from there, being basically the same
approach but with some cleaner syntax.

Thanks! I was, in fact, thinking about the problem in a very similar
trails.Without going into too much details (that would be too long)
i can reveal that once i've read your answer, i realized that i (as
well as you, of course) was very close to the solution.
 

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