interface

B

Bhuwan Bhaskar

Hi,

How can I use the functions in class / object if they are same in interface.
Code is attached. I want to implement function f of i1 and i2 in the
objcet.

Thanks and regards.

Bhuwan



interface i1

{

string f();

}

interface i2

{

string f();

}

public class t : i1, i2

{

string i1.f()

{

return ("First");

}

string i2.f()

{

return ("Second");

}
 
A

Andrew Faust

You have to caste them to an object of the same type of the interface. From
your example, you would do this:

t mainObject = new t();
Console.WriteLine(((i1) t).f());

or

t mainObject = new t();
i2 two = t;
Console.WriteLine(two.f());
 
B

Bhuwan Bhaskar

Thanks Andrew, it works with little change,
i2 two = t;

i2 two = mainObject;

Thanks
Bhuwan

Andrew Faust said:
You have to caste them to an object of the same type of the interface.
From your example, you would do this:

t mainObject = new t();
Console.WriteLine(((i1) t).f());

or

t mainObject = new t();
i2 two = t;
Console.WriteLine(two.f());

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


Bhuwan Bhaskar said:
Hi,

How can I use the functions in class / object if they are same in
interface. Code is attached. I want to implement function f of i1 and i2
in the objcet.

Thanks and regards.

Bhuwan



interface i1

{

string f();

}

interface i2

{

string f();

}

public class t : i1, i2

{

string i1.f()

{

return ("First");

}

string i2.f()

{

return ("Second");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top