views:

59

answers:

6

I'm working on building my own base user interface classes. On them, I want them to all have similar "common" properties and methods. I could define an interface class, but interface appears to only allow abstract methods, and no properties.

I don't want to copy the otherwise exact same code to each class, but not sure how to implement... Ex: I want this common stuff to be applicable to Buttons, Textbox, Checkbox, Listbox, etc user controls.

Suggestions???

+1  A: 

In this situation I usually use abstract classes. Create your base abstract class and then inherit it from your new controls.

BobbyShaftoe
A: 

You can define properties on interfaces: interface properties

public interface ISampleInterface
{
    // Property declaration:
    string Name
    {
        get;
        set;
    }
}
Welbog
A: 

You can declare a property in an interface as below:

public interface IMyInterface
{
   string Name { get; set; }
   int Age { get; set; }
}

However, in this situation it sounds like an abstract class would be better.

Razzie
A: 

It's definitely possible to specify a property on an interface

interface IFoo {
  string Name { get; set; } 
  string Age { get; } // Read Only
}

But otherwise you are correct. Interfaces specify no behavior and hence can only define "abstract" methods and properties. The implementation must be done on every single implementor. That's the price that is paid for flexibility in interfaces.

If the behavior is truly identical between all of the child classes then I usually go for an abstract class. Or often a combination. Namely define an interface and a base implementation which implements that interface.

class Foo : IFoo { 
  private string _name;
  public Name { get { return _name; } set { _name = value; } }
  public Age { get { return 42; } }
}

This allows you the flexibility of a quick implementation with the choice of usincg an interface for classes that for some reason cannot derive from Foo.

JaredPar
A: 

.Net doesn't allow multiple inheritance, but you can use an "inheritance hierarchy" to organize your base classes. This is how .Net itself is laid out.

Rich
A: 

I myself come from a C++-background where multi-inheritance is allowed and used extensibly, so I often ran into the same problems as you. The first thing you need to do is to read up on mix-ins - here you'll prolly notice that you've used them yourself all along, without ever naming them. Second step is to start to recognize your mixins whenever you need them, and you'll often find out that you might as well use them via composition. Third step is to implement them using composition... Yes, i hate this too, but there's no way around it if you wanna go .NET (or Java or...) :)

What you should use inheritence for is not for your mixins, but for stuff that actually identifies your items. I recommend looking at the .NET hierachy for some of the common controls (textbox and the likes) to get some inspiration.

Good luck

cwap
Although the other responses were good, and on a track I was thinking, your about true multi-class inhereitance is more what I was looking for. I don't want to have to keep explicitly putting in the identical code for each property that does the same thing over and over per class using it.
DRapp