473,399 Members | 4,192 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,399 software developers and data experts.

How to inherit if base class is burned?

Say I have several classes that have implemented the same code to do
something. Perhaps its connection information. They all have inherited
some class and not necessarily the same one. I'd like to take the
duplicated code and reuse it. I can't put it into ClassA, for example,
and allow these classes to inherit ClassA since they have used the one
inheritance slot. I could make ClassA a singleton, since ClassA needs
state.

Besides the singleton, how else can I reuse this code? Basically, I
need a file include (used so much in Cold Fusion and SHTML files).

Thanks,
Brett

Jul 7 '06 #1
5 1113
Brett Romero <ac*****@cygen.comwrote:
Say I have several classes that have implemented the same code to do
something. Perhaps its connection information. They all have inherited
some class and not necessarily the same one. I'd like to take the
duplicated code and reuse it. I can't put it into ClassA, for example,
and allow these classes to inherit ClassA since they have used the one
inheritance slot. I could make ClassA a singleton, since ClassA needs
state.

Besides the singleton, how else can I reuse this code? Basically, I
need a file include (used so much in Cold Fusion and SHTML files).
Specify an interface with the appropriate behaviour, and implement the
interface by delegating to an aggregated instance of another class
which has the behaviour you want.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 7 '06 #2
Thanks Jon. Can you give a small example of what this would look like?

Brett

Jul 8 '06 #3
Brett Romero <ac*****@cygen.comwrote:
Thanks Jon. Can you give a small example of what this would look like?
Sure:

public interface IFoo
{
void Bar();
}

public class Foo : IFoo
{
public void Bar()
{
Console.WriteLine ("Bar!");
}
}

public class SomethingElse : AnotherClass, IFoo
{
IFoo m_foo;

public SomethingElse()
{
// Alternatively, the implementation could be
// given to the class in the constructor or a property
m_foo = new Bar();
}

public void Bar()
{
m_foo.Bar();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 8 '06 #4
Most AOP (Aspect Oriented Programming) frameworks such as naspect or aspect#
support doing this dynamically (at runtime) as well so you never write any
code such as this, they write this code for you. This is an AOP concept
known as an inner type member or "mixin" (See background on mixin
http://en.wikipedia.org/wiki/Mixin)

This pattern is also known as an interface/implementor pattern.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Brett Romero <ac*****@cygen.comwrote:
>Thanks Jon. Can you give a small example of what this would look like?

Sure:

public interface IFoo
{
void Bar();
}

public class Foo : IFoo
{
public void Bar()
{
Console.WriteLine ("Bar!");
}
}

public class SomethingElse : AnotherClass, IFoo
{
IFoo m_foo;

public SomethingElse()
{
// Alternatively, the implementation could be
// given to the class in the constructor or a property
m_foo = new Bar();
}

public void Bar()
{
m_foo.Bar();
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jul 9 '06 #5
Thanks Jon. After seeing the code, how can it work for this sceario?
I have two assemblies
dllA
dllB

dllB file references dllA but not the other way around. I have
dllB.ClassB.UserId

dllA needs the value in UserId and will return it via
dllA.State.UserId.
>From what I see in the code you posted, dllA will need to new up
ClassB, which isn't possible since dllA doesn't reference ClassB. Is
there a way around that?

Thanks,
Brett

Jul 13 '06 #6

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

Similar topics

7
by: bobsled | last post by:
For class A to reuse functionality of a class B, A could either contains B or inherits from B. When should pick which? In a book the author says that "Don't inherit from a concrete class." Does...
5
by: Ernst Murnleitner | last post by:
Hello, is it possible to derive from std::vector and derive also its iterator? If I do it like in the example below, I get a problem when I need the begin of the vector: begin() returns the...
5
by: john conwell | last post by:
I'm trying to make a class that already inherits from a base class, also inherit from an interface in managed C++. so my interface looks like so: __gc interface ITask { __property String*...
0
by: Janaka | last post by:
I'm using Web Matrix and a custom built DLL which I've placed in my /bin directory for my website on a Windows 2003 server. I'm using the same setup which worked previously on Windows 2000. Any...
4
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object...
7
by: Frank | last post by:
Hi, a question probably asked before, but I can't find the answers. Base class X, classes A, B and C inherit class X. In class A I do not want to inherit property (or function or method) P1....
11
by: Nathan Laff | last post by:
I have a class called Company. I now want to make for one particular app a CompanyEx class, which will inherit from Company. So class CompanyEx: Company how do I set the base? kind of...
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
2
by: Joe | last post by:
Is it possible to inherit from a UserControl? If I try my user control class is not recognized. Thanks, Joe
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.