473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Class structure question - how to inherit properly?

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?

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Jul 25 '08 #1
2 1481
On Fri, 25 Jul 2008 16:09:41 -0700, K Viltersten <tm**@viltersten.com>
wrote:
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?
My first thought is that if classes A1 and B1 have the method but don't
share a base class, while classes B1 and B2 share their immediate base
class but don't share the method, that there may be something broken with
your inheritance hierarchy.

But let's take as granted that the hierarchy is sound. Then the question
is how to share the implementation. But that's hard to answer without
knowing what the method actually does. What is it about the method that
causes it to be in both A1 and B1? Is it related to the base class S that
they share? If so, what is it about class B2 that causes it to not have
the same method?

If the method is unrelated to the base class S, then the question is
what's similar about A1 and B1 that causes them to have the same method?
Does the method need access to private/protected members in classes A1 and
B1? If not, do you think maybe it would make sense to create extension
methods for the classes, where the extension methods both call a shared
implementation?

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.

But without knowing more about the design, I don't see a way to provide a
really insightful answer.

Pete
Jul 25 '08 #2
>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 {...}
<snip>
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.

--
Regards
Konrad Viltersten
Jul 26 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
6
by: Dan H. | last post by:
Hello, Is there any advantage of using a struct or a class as part of public delegate bool MyEvent(object source, SomeEventArgs e); Struct: public struct SomeEventArgs { public float...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
3
by: David Lozzi | last post by:
Howdy, I've discovered how to create and use a class in ASP.NET. However, when is the best time to use a class? For example, I am currently using session variables to store user information (user...
13
by: Scott Meddows | last post by:
I'm writing an NT service to provide security information enterprise wide. I would like to have all of this information cached memory if a previous request loaded it into memory. Then using a...
13
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes....
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.