Home All Groups Group Topic Archive Search About
Author
30 Oct 2007 5:24 AM
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");

}

Author
30 Oct 2007 6:04 AM
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());

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


Show quote
"Bhuwan Bhaskar" <k***@gmail.com> wrote in message
news:O$DSZUrGIHA.4916@TK2MSFTNGP02.phx.gbl...
> 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");
>
> }
>
>
Author
30 Oct 2007 7:18 AM
Bhuwan Bhaskar
Thanks Andrew, it works with little change,

> i2 two = t;

i2 two = mainObject;

Thanks
Bhuwan

Show quote
"Andrew Faust" <and***@andrewfaust.com> wrote in message
news:4218C044-94C1-4C81-9C36-347AD7B6CE08@microsoft.com...
> 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" <k***@gmail.com> wrote in message
> news:O$DSZUrGIHA.4916@TK2MSFTNGP02.phx.gbl...
>> 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");
>>
>> }
>>
>>
>

AddThis Social Bookmark Button