Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I have one problem i have two interfaces

C#
interface test1
{
void show();
}
interface test2
{
void show();
}

class testing: test1,test2
{
// How to call test1 and test2 method
}


Can any one help me please?

Regards
S. keshaavan
Posted
Updated 13-May-12 0:19am
v2

OriginalGriff: explicit interface methods should not be public (you will get compile error if you declare any access modifier, but I wonder did you check your code before you wrote the answer?) but it is private by default. The reason being is that these methods should be accessible via the interface reference not via the object of the class.

So, here is how we implement (explicit implementation) the interfaces which happened to be having same methods.

C#
class testing : test1, test2
    {

        void test1.show()
        {
            MessageBox.Show("From interface test1");
        }

        void test2.show()
        {
            MessageBox.Show("From interface test2");
        }
    }


And here is how you access them:

C#
testing t = new testing();

            test1 t1 = t;
            test2 t2 = t;

            t1.show();
            t2.show();
 
Share this answer
 
Comments
bbirajdar 5-Jul-12 7:58am    
@Prabhu Ram.. You are absolutely correct... The implementation should not be public....It gives the error "Error 1 The modifier 'public' is not valid for this item ....testing.cs " .. My +5
You can't - they are interfaces.
Interface do not define any code, they just declare a contract that the derived class must adhere to by implementing the properties and methods.

You would have to implement the show method in the testing class and then if you needed to explicitly call it, cast your testing instance to test1 or test2:
C#
interface test1
    {
    void show();
    }
interface test2
    {
    void show();
    }

public class testing : test1, test2
    {
    // How to call test1 and test2 method
    public testing()
        {
        ((test1)this).show();
        ((test2)this).show();

        }

    public void test1.show()
        {
        throw new NotImplementedException();
        }

    public void test2.show()
        {
        throw new NotImplementedException();
        }
    }
 
Share this answer
 
Comments
VJ Reddy 13-May-12 6:57am    
Good answer. 5!
Maciej Los 13-May-12 12:55pm    
I didn't expect it that answer is so "simple" ;)
+5!
I would have first looked at Google. This has been asked infinte times already.

https://www.google.com/search?sourceid=chrome&ie=UTF-8&q=Multiple+inheritence+c%23&hl=en[^]

Anyway, this thread should help.

http://stackoverflow.com/questions/178333/multiple-inheritance-in-c-sharp[^]
 
Share this answer
 
Interface 1
C#
public interface test1
{
    void show();
}


Inerface 2
C#
public interface test2
{
    void show();
}

Implementing the interfaces
C#
public class testing: test1,test2
{
	public testing()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    #region test1 Members

    void test1.show()
    {
        throw new NotImplementedException();
    }

    #endregion

    #region test2 Members

    void test2.show()
    {
        throw new NotImplementedException();

    }

    #endregion
}


Creating the object and calling the methods show()
C#
testing test = new testing();
        ((test1)test).show();
        ((test2)test).show();
 
Share this answer
 
 
Share this answer
 
v2

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