473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Interface and casting

How can I do the following....

interface iface
{
int someMethod();
}

class c1 : iface
{
int someMethod() {return 10;}
}

class c2 : iface
{
int someMethod() {return {return 20;}
}

class Test
{
int testMethod()
{
return testInterface(new c1());
}

int testInterface(iface i)
{
// in this case, returns 10
// as it stands now, i have to
// cast it to a c1, before it will
// return correct value;
return i.testMethod
}
}

Thanks

Nov 15 '05 #1
3 1261
anon <an*******@discussions.microsoft.com> wrote:
How can I do the following....

interface iface
{
int someMethod();
}

class c1 : iface
{
int someMethod() {return 10;}
}

class c2 : iface
{
int someMethod() {return {return 20;}
}

class Test
{
int testMethod()
{
return testInterface(new c1());
}

int testInterface(iface i)
{
// in this case, returns 10
// as it stands now, i have to
// cast it to a c1, before it will
// return correct value;
return i.testMethod
}
}


Firstly, please post code which will compile - the above won't compile
for various reasons, which makes it difficult to tell what you actually
want to happen. Casting i to a c1 won't help at all, as neither iface
nor c1 declare a method testMethod. The two implementations of
someMethod also need to be public.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
"anon" <an*******@discussions.microsoft.com> wrote in message
news:f6****************************@phx.gbl...
How can I do the following....

interface iface
{
int someMethod();
}

class c1 : iface
{
int someMethod() {return 10;}
}

class c2 : iface
{
int someMethod() {return {return 20;}
}

class Test
{
int testMethod()
{
return testInterface(new c1());
}

int testInterface(iface i)
{
// in this case, returns 10
// as it stands now, i have to
// cast it to a c1, before it will
// return correct value;
return i.testMethod
}
}

The only thing you can do in this case is call someMethod() on i:

int testInterface(iface i)
{
return i.someMethod();
}

because testMethod is not a member of iface. If you wanted to call
testMethod you would have to add it to iface and then add it to c1 and c2.

I don't believe you would want to cast i to c1 in testInterface. That's
because as soon as you passed a c2 or other iface derived type, you would
get an InvalidCastException.

Joe
--
http://www.csharp-station.com
Nov 15 '05 #3
I'm sorry Joe,

I made some mistakes when i originally posted.. please
see my new post ( interface and casting (second try) )

Thanks
Kurt
-----Original Message-----
"anon" <an*******@discussions.microsoft.com> wrote in messagenews:f6****************************@phx.gbl...
How can I do the following....

interface iface
{
int someMethod();
}

class c1 : iface
{
int someMethod() {return 10;}
}

class c2 : iface
{
int someMethod() {return {return 20;}
}

class Test
{
int testMethod()
{
return testInterface(new c1());
}

int testInterface(iface i)
{
// in this case, returns 10
// as it stands now, i have to
// cast it to a c1, before it will
// return correct value;
return i.testMethod
}
}

The only thing you can do in this case is call someMethod

() on i:
int testInterface(iface i)
{
return i.someMethod();
}

because testMethod is not a member of iface. If you wanted to calltestMethod you would have to add it to iface and then add it to c1 and c2.
I don't believe you would want to cast i to c1 in testInterface. That'sbecause as soon as you passed a c2 or other iface derived type, you wouldget an InvalidCastException.

Joe
--
http://www.csharp-station.com
.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
4
by: Shane | last post by:
I am having a problem where I create an instance of a class from an assembly and I try to cast it to an interface that it inherits from and it says that the cast is invalid. Here is the code: ...
15
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } ...
3
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i...
10
by: Bob | last post by:
This has been bugging me for a while now. GetType isn't availble for variables decalred as interface types, I have to DirectCast(somevariable, Object). In example: Sub SomeSub(ByVal...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
2
by: gunters | last post by:
Can please someone enlight me, I don't understand this: It is possible in C# to "reach" inside a boxed value type through an interface like in the following code: interface IPerson { string...
8
by: Lamefif | last post by:
// C3DRect supports IDraw and IShapeEdit. class C3DRect : public IDraw, public IShapeEdit { public: C3DRect(); virtual ~C3DRect(); // IDraw virtual void Draw(); // IShapeEdit virtual void...
5
by: Russell Mangel | last post by:
Here is something much closer to what I was trying to do. Thanks to Arne, and Peter and all who helped. Sorry I didn't explain my question better. Russell Mangel Las Vegas, NV // Begin...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.