interface

  • Thread starter Thread starter Bhuwan Bhaskar
  • Start date Start date
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");

}
 
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());
 
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");

}
 
Back
Top