Differences between C# and VB "Implements"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    Differences between C# and VB "Implements"

    Hey, I've been trawling documentation for this, but can't figure out what it's called so I'm running short of answers:

    In VB say you build some interface:
    Code:
    Interface IDemoInterface
     
      Sub MyFirstMethod(ByVal Param1 As String)
      Sub MySecondMethod(ByVal Param1 As String)
      Sub MyThirdMethod(ByVal Param1 As Integer)
     
    End Interface
    Now, you get as far as implementing that interface:
    Code:
    Public Class DemoImplementsInterface
      Implements IDemoInterface
    Now, as soon as you hit enter after typing the interface name that you're implementing, it automatically populates the class with the relevant methods required to implement that class:

    Code:
    Public Class DemoImplementsInterface
      Implements IDemoInterface
     
      Public Sub MyFirstMethod(ByVal Param1 As String) Implements IDemoInterface.MyFirstMethod
     
      End Sub
     
      Public Sub MySecondMethod(ByVal Param1 As String) Implements IDemoInterface.MySecondMethod
     
      End Sub
     
      Public Sub MyThirdMethod(ByVal Param1 As Integer) Implements IDemoInterface.MyThirdMethod
     
      End Sub
     
    End Class
    So, I've just realised that this appears not to work in C# causing me a huge and largely unecessary amount of typing. Is there a way of forcing this to work? I thought about writing a macro to parse the interface and add the methods into my code, but it seems like a lot of work for something that at least in my mind should be automatic. Does anyone know what this process of automatically writing out the interface methods in your class is called? Maybe if I had that magic keyword, I could figure out the rest myself.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That is often an annoyance of mine too. Eclipse does it for java, why doesn't it work in C#.
    Well it does, just you have to be paying really close attention.
    The first letter of your interface will get a little blue line under it (like the red line when you forget to add a namespace for something) and you get two options to implement the namespace. My quick test did not show a difference in picking either option, but there must be some difference.

    Comment

    • nateraaaa
      Recognized Expert Contributor
      • May 2007
      • 664

      #3
      Not sure if either of you use ReSharper but ReSharper will create all methods associated with the Interface that you are implementing simply by clicking on the red light bulb and selecting Implement members. If you haven't used ReSharper before I highly recommend it.

      ReSharper is the most popular extension on Microsoft Visual Studio Marketplace with 2.5M+ downloads and has been boosting developer productivity since 2004

      Nathan

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Thanks guys - both of your posts helped. ReSharper looks like a fairly comprehensive tool - but at $349 for the license, I think I'll just pay more attention to the little marks (what are they called?) in my IDE and use the option that Plater pointed out. Maybe I'll check out ReSharper down the road when some of the other tools become of more use to me.

        Comment

        Working...