2

I was wondering if there was a way to get Visual Studio to fill in a default Interface Implementation when it's implemented.

This particular client requires the use of VB, please excuse me.

Here's an example:

I have an interface:

Public Interface ISavableReport
    Sub SetReport(R As Report)
End Interface

In almost all of the classes which will implement the interface, the SetReport method is the same. Of course, I would never want to be able to define methods in an interface since that would defeat about 2/3 of the reasons for an interface, but is there some way to get Visual Studio to fill in a default implementations?

In VB for example, when I type:

Implements ISavableReport

It will automatically create the property/method/event stubs in the class:

Public function SetReport(R As Report) Implements ISavableReport.SetReport

End Function

I would like it to automatically fill in:

Public function SetReport(R As Report) Implements ISavableReport.SetReport
  _localReport = R
End Function

I've never attempted to customize Visual Studio itself, and frankly this feature has a very small domain of use, but For certain projects I've worked on where the majority of the implementing classes were nearly identical (even the local vars) It would help me out instead of having to copy/paste so much.

Just a thought.

Paul

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Paul Perrick
  • 496
  • 9
  • 22
  • The code that does that will put you out of a job. It doesn't exist. Yet. – Hans Passant Aug 12 '11 at 10:30
  • I just want to be clear on something. This would not be code. This would be a "template" in Visual Studio. I would not subject another programmer to some default Interface implementation. – Paul Perrick Aug 14 '11 at 06:51
  • Never mind guys. I'm sorry I phrased the title of this thread the way I did. I did not want a default implementation in code. What I wanted was to insert a snippet of code and I hadn't thought that VS already has that feature built in: http://msdn.microsoft.com/en-us/library/ms165394%28v=vs.80%29.aspx – Paul Perrick Oct 20 '11 at 07:53

2 Answers2

2

If all your classes are almost identical I suggest you introduce a base class.

riezebosch
  • 1,950
  • 16
  • 29
  • 1
    That's the reason this is an issue. I am already inheriting a base class written by someone else. It's a reporting class and each report is created in a designer which spits out Class (which already inherits from a base report class). I need a way to set/retrieve settings etc. from this class. I needed to implement in interface to pass these reports around. I'm just looking for a way to create templates in Visual Studio. – Paul Perrick Aug 14 '11 at 06:54
0

Unfortunately there is no such support. You can either have all implementors of an interface derive from a common base that implements it, or you can put the implementation into a static method somewhere and have all the implementing types (types using your interface) simply delegate to the static method.

If you desperately want to do this, the following question tackles it too:

How to define the default implementation of an interface in c#?

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
  • Unfortunately I don't want to define a default implementation for an interface. – Paul Perrick Aug 03 '12 at 05:46
  • In that case you could create your own snippet that is just the interfaces methods. Then instead of right clicking to implement interface, you type and tab in your snippet to the file. – Adam Houldsworth Aug 03 '12 at 07:10