Click here to Skip to main content
16,011,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
interface iSame
    {
        void Method();
    }
    interface iSame1
    {
        int Method();
    }

    public partial class Form1 : Form, iSame, iSame1
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Method()
        {
            MessageBox.Show("This is iSame");
        }

        public int Method()
        {
            MessageBox.Show("This is iSame");
            return 1;
       }


How can i implement a this application with same method names of different interfaces ?

SQL
This gives an error saying already Method is implemented. But if you remove the int Method then also it gives compilation error saying Error    2   'WindowsFormsApplication1.Form1' does not implement interface member 'WindowsFormsApplication1.iSame1.Method()'. 'WindowsFormsApplication1.Form1.Method()' cannot implement 'WindowsFormsApplication1.iSame1.Method()' because it does not have the matching return type of 'int'.  F:\C#\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    22  26  WindowsFormsApplication1

If both Tests have the same signature, same return type but differ in the types of exceptions they throw, then the class implements only the one Test, but it must contain the exceptions both Test's

If arguments are different we can implement without any error.
Posted

First of all, the methods are not the same; they only have the same name and signature. Method identity is made of the combination of assembly/type/method, not just name. If an assembly is signed, it even makes each method world-unique, even if other methods have the same name/signature.

You simply need to write explicit interface implementations instead of implicit. Generally, the explicit provides many benefits over the implicit.

Here is how:
C#
interface IFirst { void Method(); }
interface ISecond { void Method(); }

class SomeClass: Form, IFirst, ISecond { // here is where the problem starts
    //this is how it is solved:
    void IFirst.Method() { /* ... */ }
    void ISecond.Method() { /* ... */ }
} //class SomeClass

// now, the instances:

SomeClass classInstance = new SomeClass();
// you cannot use IFirst or ISecond members without a type cast with this variable,
// which is good, because it provides proper isolation

IFirst classInstanceAsIFirst = classInstance; // can use IFirst members
ISecond classInstanceAsISecond = classInstance; // same runtime object, but now you can use ISecond members


I hope this is all clear.

See also this old but still fully valid MSDN article: http://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx[^].

Most recent chapters in MSDN manual:
http://msdn.microsoft.com/en-us/library/ms173157.aspx[^],
http://msdn.microsoft.com/en-us/library/tx1s9z1w.aspx[^].

—SA
 
Share this answer
 
v4
Comments
code4Better 26-Jun-14 21:16pm    
Thanks for your time Sergey. This works fine now. Shared MSDN doc is also a gud one.
Sergey Alexandrovich Kryukov 26-Jun-14 23:04pm    
My pleasure; I'm glad it helps you.
Good luck, call again.
—SA
Animesh Datta 27-Jun-14 0:43am    
very good explanation Sir . thank you . My +5.
This code project article Interfaces in C# (For Beginners)[^] has a section on this very problem (look for "P13.cs" in the article)
 
Share this answer
 
Comments
code4Better 26-Jun-14 9:32am    
Hi Chill, thank you for the answer. The link what you have provided has same interface methods with same return types where as in my case the return type of the interface methods are different. That is the reason i am geting error.Do you have any more input on this ??
Sergey Alexandrovich Kryukov 26-Jun-14 14:48pm    
Not a very good article, I think (and it even violated good naming conventions :-); and it does not really points out what to use to solve the problem.
If you simply pointed out a chapter in original Microsoft documentation, it would be more useful.
Please see my answer.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900