Re: Difference between casting to a class and to an interface
From: Mark Broadbent (no-spam-please_at_no-spam-please.com)
Date: 07/04/04
- Next message: Nick: "Re: File/Exit howto"
- Previous message: Chua Wen Ching: "RE: File/Exit howto"
- In reply to: yufufi: "Difference between casting to a class and to an interface"
- Next in thread: Niki Estner: "Re: Difference between casting to a class and to an interface"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 4 Jul 2004 15:22:19 +0100
that is a very good shout.
I was under the impression that you could only cast an object to an
interface if said object implementated said interface.
After having a play I see (like you) that this is not true.
It seems that the compiler does not type check a cast to an interface
(whether this is by design or not I do not know?)
However in the runtime the cast is succeeding.
Unfortunately the methods exposed by the interface will not succeed if
accessed because of the incompatible object type -so when you try and run
one through the interface ...BANG! exception.
I can see this functionality of being of some benefit in some instances, but
I would love to know whether this is by design.
Nice post, well done!
-- Br, Mark Broadbent mcdba , mcse+i ============= "yufufi" <yufufi@ttnet.net.tr> wrote in message news:OGtFRlcYEHA.1048@tk2msftngp13.phx.gbl... > 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; > > but it complains for the following lines > shape b= new shape(); > int a; > a=(int)b; > > error is Cannot convert type 'shape' to 'int' > > is there difference between casting to a class and casting to an interface > ?? what does the compiler checks when it is casting to an interface (or does > it check anything ?) > > Thanx a lot, > > yufufi > > > > > > > >
- Next message: Nick: "Re: File/Exit howto"
- Previous message: Chua Wen Ching: "RE: File/Exit howto"
- In reply to: yufufi: "Difference between casting to a class and to an interface"
- Next in thread: Niki Estner: "Re: Difference between casting to a class and to an interface"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|