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