How do I share non-static code when I have to use different base classes?

W

Wysiwyg

I'm new to c# programming and can't figure out how to avoid duplicating
common code in multiple classes when I'm restricted to using different
system base classes.. I'm using c# in asp.net to write a web application but
this isn't specifically a web question.

I have two classes which must inherit a different System class, in a
specific case my web pages must inherit System.Web.UI.Page while my user
controls must inherit System.Web.UI.UserControl. I have a significant number
of methods I want to include in both classes which are instantiated, i.e.
they can't be static, so I can't just put the code in an abstract class.
Interfaces are just a contract of methods, properties, etc. that must be
defined but they don't implement anything so that doesn't seem to help me.

So, if I have two different classes which must each be based on a different
System base class how do I share code between the two without simply
duplicating it? Note that not all of the methods are the same in the classes
I've written, just most of it.

Thanks, any help is appreciated.
Bill
 
J

Jon Skeet [C# MVP]

Wysiwyg said:
I'm new to c# programming and can't figure out how to avoid duplicating
common code in multiple classes when I'm restricted to using different
system base classes.. I'm using c# in asp.net to write a web application but
this isn't specifically a web question.

I have two classes which must inherit a different System class, in a
specific case my web pages must inherit System.Web.UI.Page while my user
controls must inherit System.Web.UI.UserControl. I have a significant number
of methods I want to include in both classes which are instantiated, i.e.
they can't be static, so I can't just put the code in an abstract class.
Interfaces are just a contract of methods, properties, etc. that must be
defined but they don't implement anything so that doesn't seem to help me.

So, if I have two different classes which must each be based on a different
System base class how do I share code between the two without simply
duplicating it? Note that not all of the methods are the same in the classes
I've written, just most of it.

Usually I'd encapsulate the common code in another class which
describes it, and create an instance of that class in each of the two
separate classes. Of course, each case is different to some extent.
 
N

Nick Malik [Microsoft]

Hi Bill,

The most common method is the one that Jon describes. Create a different
object heirarchy that has all the methods you need, and when you construct
the class of System.Web.UI.Page or System.Web.UI.UserControl, create one of
these objects and store the instance in a private class member.

If you need these methods to be exposed as public methods of the Page and
UserControl objects, then here are the steps:

a) create an interface that defines the public methods (call it ICommon for
the sake of this description)
b) insure that your Page and UserControl object inherit from ICommon
c) Create one or more classes that inherit only from ICommon and implement
the methods and properties.
(call the ConcreteCommonPage() and ConcreteCommonUC() for the sake of
this description)
d) Declare, in your Page and UserControl classes: private ICommon _Common;
e) In the constructor for your Page class, create a ConcreteCommonPage()
instance and assign it to the private _Common;
f) in the constructor for your UserControl class, create a
ConcreteCommonUC() instance and assign it to the private _Common;
g) Add a property with only a Get section to both classes as follows:
public property ICommon Common
{ Get { return _Common; } }

Now, every place in your code, where someone needs a common method or
property, they will reference it this way:
MyUserPage.Common.MyCommonMethod()
MyUserControl.Common.MyCommonMethod()

This is how we get around the restrictions against multiple inheritance :).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
G

Guest

Thanks Nick! That makes perfect sense.

Nick Malik said:
Hi Bill,

The most common method is the one that Jon describes. Create a different
object heirarchy that has all the methods you need, and when you construct
the class of System.Web.UI.Page or System.Web.UI.UserControl, create one of
these objects and store the instance in a private class member.

If you need these methods to be exposed as public methods of the Page and
UserControl objects, then here are the steps:

a) create an interface that defines the public methods (call it ICommon for
the sake of this description)
b) insure that your Page and UserControl object inherit from ICommon
c) Create one or more classes that inherit only from ICommon and implement
the methods and properties.
(call the ConcreteCommonPage() and ConcreteCommonUC() for the sake of
this description)
d) Declare, in your Page and UserControl classes: private ICommon _Common;
e) In the constructor for your Page class, create a ConcreteCommonPage()
instance and assign it to the private _Common;
f) in the constructor for your UserControl class, create a
ConcreteCommonUC() instance and assign it to the private _Common;
g) Add a property with only a Get section to both classes as follows:
public property ICommon Common
{ Get { return _Common; } }

Now, every place in your code, where someone needs a common method or
property, they will reference it this way:
MyUserPage.Common.MyCommonMethod()
MyUserControl.Common.MyCommonMethod()

This is how we get around the restrictions against multiple inheritance :).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

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