473,322 Members | 1,409 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,322 software developers and data experts.

interface and casting (second try)

How can I do the following....

interface iface
{
int someMethod();
}

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

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

class c3 : c1
{
int new someMethod() {return 30;}
}

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

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
}
}

If i pass in a c1, I would like to return 10, however if
I pass in a c3, I would like it to return 30. Right now,
when passing in a c3, it returns 10. This is not what I
expected, although I think i understand why this happens.

I would like to do this without have to figure out which
type was passed in... I would think that if I passed in a
c3, then c3's method would be called.. and not c1's
method.

Thanks

Nov 15 '05 #1
3 1043
why are you using new ?
"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 : c1
{
int new someMethod() {return 20;}
}

class c3 : c1
{
int new someMethod() {return 30;}
}

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

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
}
}

If i pass in a c1, I would like to return 10, however if
I pass in a c3, I would like it to return 30. Right now,
when passing in a c3, it returns 10. This is not what I
expected, although I think i understand why this happens.

I would like to do this without have to figure out which
type was passed in... I would think that if I passed in a
c3, then c3's method would be called.. and not c1's
method.

Thanks

Nov 15 '05 #2

Well, I probably didn't show this correctly... I would
also like to hide "someMethod" from the user of my
class... but also make it available to the programmer of
my class... the only way to do this was to first
implement the interface, then derive from the implemented
interface.
-----Original Message-----
why are you using new ?
"anon" <an*******@discussions.microsoft.com> wrote in messagenews:f6****************************@phx.gbl...
How can I do the following....

interface iface
{
int someMethod();
}

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

class c2 : c1
{
internal int new someMethod() {return 20;}
}

class c3 : c1
{
internal int new someMethod() {return 30;}
}

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

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.someMethod
}
}

If i pass in a c1, I would like to return 10, however if I pass in a c3, I would like it to return 30. Right now, when passing in a c3, it returns 10. This is not what I expected, although I think i understand why this happens.
I would like to do this without have to figure out which type was passed in... I would think that if I passed in a c3, then c3's method would be called.. and not c1's
method.

Thanks

.

Nov 15 '05 #3
Kurt Lange <an*******@discussions.microsoft.com> wrote:
Well, I probably didn't show this correctly... I would
also like to hide "someMethod" from the user of my
class... but also make it available to the programmer of
my class... the only way to do this was to first
implement the interface, then derive from the implemented
interface.


Which version of someMethod do you want to hide?

If the method is available as part of the public interface, you could
use explicit interface implementation, but it's really not nice IMO.
Basically you should remember Liskov's Substitutability Rule - simply
put, it's that any instance of a derived class should be able to be
used as if it were an instance of the base class. Google for the rule
for more information on it.

If you could give a more concrete example of what you're trying to do,
that would help.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

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

Similar topics

3
by: Jeff Molby | last post by:
Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g> I have 3 projects in my solution: One app and 2 libraries. I am...
3
by: anon | last post by:
How can I do the following.... interface iface { int someMethod(); } class c1 : iface { int someMethod() {return 10;}
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;...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
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" ); } } ...
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...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.