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

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Wysiwyg <wy*****@xmissionNSPAM.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
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.
--
"Wysiwyg" <wy*****@xmissionNSPAM.com> wrote in message
news:cr**********@news.xmission.com...
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

Nov 16 '05 #3

P: n/a
I have a way to do "multiple inheritance 'Lite' in C#".
http://blogs.msdn.com/nickmalik/arch...31/344966.aspx
--
--- 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.
--
"Wysiwyg" <wy*****@xmissionNSPAM.com> wrote in message
news:cr**********@news.xmission.com...
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

Nov 16 '05 #4

P: n/a
Thanks Nick! That makes perfect sense.

"Nick Malik [Microsoft]" wrote:
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.
--
"Wysiwyg" <wy*****@xmissionNSPAM.com> wrote in message
news:cr**********@news.xmission.com...
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


Nov 16 '05 #5

P: n/a
Thanks! All of that is very helpful.

Bill

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:C1kBd.262340$5K2.86454@attbi_s03...
I have a way to do "multiple inheritance 'Lite' in C#".
http://blogs.msdn.com/nickmalik/arch...31/344966.aspx

--
--- 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.
--

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.