views:

60

answers:

3

I have 2 class's

Class 1.

public class BaseContentPage : System.Web.UI.Page
{

}

Class 2.

public class BaseUserControl : System.Web.UI.UserControl
{


}

And now i want them to be aware of this class.

public class BaseCommon 
{
  public string Variable1 { get; set; }
  public string Variable2 { get; set; }
  public string Variable3 { get; set; }
}

How I'm currently doing it is by making the variables of the BaseCommon class static like so

public static string Variable1 { get; set; }
public static string Variable2 { get; set; }
public static string Variable3 { get; set; }

How else can i do this?

A: 

If they are static, does that mean that this third class is a global resource? You could look at Singleton or IoC containers or pass the instance to the other classes when constructed.

You need to give more concrete examples of what this common "base" data that you are adding to two different parts of your object inheritance tree are.

Conceivably, say each instance really just has similar data (an example I'm thinking of is internationalization, say, which is used to customize both a page and a usercontrol according to phrase IDs, etc which are specific in context), then what you can do is have a class I18nSettings which implements II18nSettings. Then in each class, encapsulate an I18nSettings instance and implement II18nSettings and pass them through. Alternatively, you can expose the internal I18nSettings instance. I've even just defined an interface and had each class implement it - it was not worth having a separate class at all.

There are other options to do this kind of thing - it depends on whether the concrete classes you are inheriting from implement a lot more useful things than the things you are adding. It might make more sense to inherit from your concrete class and implement other interfaces so that the class can be used in other places.

Cade Roux
I dont want global resources thats why i didn't want to use static.
BlackTea
Expanded my notes on options for non-global objective.
Cade Roux
+1  A: 

First off, I don't think you want the BaseCommon properties to be static. The properties are then global across the application so changing them from one instance will change them for all instances.

What you're talking about is multiple inheritence and it isn't supported by c#. You'd be better off changing BaseCommon to be an interface and having BaseContentPage and BaseUserControl implement that interface.

lomaxx
+3  A: 

Use composition.

Give BaseContentPage and BaseUserControl a private field (which can be exposed as a property if needed) of type BaseCommon (don't make it static).

They can either create BaseCommon or have an instance passed in through the constructor (Dependency Injection).

TrueWill
Hey I was going to say that ;-)
Eric J.