Results 1 to 5 of 5

Thread: [RESOLVED] Classes using an interfaces extension method

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Resolved [RESOLVED] Classes using an interfaces extension method

    Hi all,

    I have an interface (IMessageDispatcher) which has an extension method (DispatchMessages) containing code that would be identical in all classes that implement it.
    I don't want to code the identical method in every class that implements the interface.
    If i leave out the method in the class i get a compiler error, class does not implement interface, but i don't want to implement it as there is an extension method already there.

    I've tried casting to the interface to call the extension method, but it still calls my class' wrapper method, so i get a stack overflow:
    Code:
    public void DispatchMessages()
    {
         (this as IMessageDispatcher).DispatchMessages();
    }
    How can i avoid writing identical code in all classes that implement this interface?

    Cheers!!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: Classes using an interfaces extension method

    DispatchMessages() should no longer be a method of IMessageDispatcher since DispatchMessages() is now an extension method.
    When you implement IMessageDispatcher you won't have to define DispatchMessages() in the class.
    You can't have the same method name in an interface and also an extension of that interface.

  3. #3

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: Classes using an interfaces extension method

    Thanks for the very quick reply.
    Now that i think about it, it is kind of obvious, it's the first time i've used extension methods on interfaces!
    Cheers for the heads up. Rep++
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  4. #4
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611

    Re: [RESOLVED] Classes using an interfaces extension method

    You could consider that you don't make it a interface, but anabstract class.
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

    Free online tools

  5. #5

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [RESOLVED] Classes using an interfaces extension method

    I couldn't use a base/abstract class because the classes that would implement the interfaces are already inheriting, but thanks for the suggestion.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width