views:

90

answers:

5

I have some user controls which I want to specify properties and methods for.

They inherit from a base class, because they all have properties such as "Foo" and "Bar", and the reason I used a base class is so that I dont have to manually implement all of these properties in each derived class.

However, I want to have a method that is only in the derived classes, not in the base class, as the base class doesn't know how to "do" the method, so I am thinking of using an interface for this. If i put it in the base class, I have to define some body to return a value (which would be invalid), and always make sure that the overriding method is not calling the base. method

Is the right way to go about this to use both the base class and an interface to expose the method? It seems very round-about, but every way i think about doing it seems wrong...

Let me know if the question is not clear, it's probably a dumb question but I want to do this right.

EDIT : Thanks to all the people with your excellent abstract suggestions, but this breaks the designer. If abstract was not a selectable option, what would you do?

+4  A: 

Alternatively you could define the method as 'abstract' in the base class, which will not require the class to implement it. For example:

abstract class A
{
   public abstract void B();
}

Of course this will force your base class to be abstract as well, but it sounds like this would work just fine for you.

See Abstract methods on MSDN.

Update

Since abstract is not an option for you due to designer issues, you could just define the method as part of your base class, and have it throw a NotImplementedException if it is called directly from the base class:

void DerivMethod()
{
    // Must be implemented by derived class
    throw new NotImplementedException();
}

Otherwise, using an interface would be fine, especially if the above leaves a bad taste in your mouth...

Justin Ethier
What's `override` there for?
Martinho Fernandes
You have to be careful creating abstract user controls. This will actually be problematic for the designer. If the designer isn't needed to modify the derived user controls, then this will work OK.
Scott P
Scott is right on this one, I need to be able to use the designer, and this makes it unable to do so. However, everything else about it works great, but this is the reason i didn't go with an abstract
Lerxst
All of these solutions work, however the same problem applies that I would either have to implement all the interface's properties in every derived class, or throw a not-implemented exception in a base class and have no compile-error if the method doesnt get implemented. Which would be better design? I guess there is no way around it...
Lerxst
I thought you only needed a solution for a method, and already had it worked out that the properties would stay in the base class?
Justin Ethier
+2  A: 

You should make your base class an Abstract class. Then the base class can implement the Interface by marking the method abstract.

http://msdn.microsoft.com/en-us/library/aa664435(VS.71).aspx

sgriffinusa
+1  A: 

Mark the method as abstract in your base class. You'll be forced to implement it in the derived classes, but the base class will not need to have a method definition.

Paul Kearney - pk
A: 

Declare the function signature in the base class and use the "abstract" modifier.

Stargazer712
+1  A: 

I agree with with others, but making your user control abstract has some issues for the designer. The designer will often not display the abstract user control.

I would implement the interface methods in the base class. You can throw a NotImplemented exception or Assert.Fail in the methods if you want to make sure the inheritors are overriding these methods properly.

Scott P