Do you know a quick way to implement method(s) from an Interface to a Class. If yes, how can you do it?
Situation : I have an Interface used by over 15 concrete classes. I added a new method and I need to implement this new method in all concrete class.
Update
All my concrete class implement the interface and all the method fine. Later, I add a new method in the interface. To be able to compile, I need to implement the new method in all class. I do not want to go 1 by 1 on each class to implement the method. Is there a way, like "Right clicking the new method" in the interface that will go in all concrete class and all automaticly the new method. This way I will not have to open all class?
-
Provide an abstract base class with a default implementation, and then have all your concrete classes inherit that abstract class.
Ian Varley : Don't know why this was -1 ... that's the right answer. Unless you were looking for "copy and paste it".Joel Coehoorn : Maybe Daok was looking for an IDE help, rather than a code solution.Daok : I cannot inherit abstract class. Class can inherit only 1 class and many Interface. I am more searching a way to do it, see Aku answer. This solution is not what I search.Joel Coehoorn : Your abstract class could also inherit anything else you need.Daok : Not all these concretes classes inherit the same class. So I would have to create many Abstract Class. This will become bloaty for no real reason. -
yes,
immplement the interface class in each of the 15 classes. Example:
public class MyClass:MyInteface{ }
afterwards, Hover over the first letter of the interface name, and select from dropdown "Explicitly implement interface ".
I am assuming you are using Visual Studio.
Daok : I know that way... I am searching something faster.Paige Watson : While this is a nice way to stub out the class functions, the IDE will add an exception ("This functionality has not yet been implemented" or something) to the function. Make sure that you remove this if your class is not going to do anything with the added function or you'll have problems later. -
Since you mentioned that you have ReSharper installed, here some way to quickly implement this:
- Use "Find Usages Advanced" with "Implementations" checkbox checked
- For each class use quick action "Implement members"
Also you can use "solution wide analysis" feature of ReSharper - it will quickly find all classes that don't implement this new method
EDIT:
Finally I found a really quick way:
- Save method signature in clipboard.
- Position cursor on Boo in IFoo interface (notice code error - empty code block, this is intentional).
- Right click and choose Refactor->"Push Members down"
- Select needed classes in the shown dialog box and click Next.
Restore method signature from clipboard
internal interface IFoo { void Boo() { } }
class Boo:IFoo { }
class Foo: IFoo { }
Daok : This is the only answer for the moment that look "fast". Thx aku.Mark Brittingham : ReSharper is an amazing tool, eh? You *did*, however, steal my answer ;-)
0 comments:
Post a Comment