Board index » delphi » Casting Object as Interface (D3)
mi...@gte.net
![]() Delphi Developer |
Fri, 22 Oct 1999 03:00:00 GMT
|
mi...@gte.net
![]() Delphi Developer |
Fri, 22 Oct 1999 03:00:00 GMT
Casting Object as Interface (D3)I am trying to cast an object as an interface at runtime in D3, and not According to the docs with D3, this should work... ... type var implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); Dog := AnObject; // Doesen't compile Any thoughts? Mike |
Conrad Herrman
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)mi...@gte.net wrote in article <5kljgl$kp...@news10.gte.net>... Quote> I am trying to cast an object as an interface at runtime in D3, and not a class derived from TInterfacedObject. TInterfacedObject doesn't actually have the ability to retrieve interfaces on itself, until you introduce at least one interface into the class, therefore the first assignment and third assignments won't work. The interface must have a GUID, else you can't QueryInterface for it. The following unit, which is derived from yours, compiles correctly. unit Unit1; interface uses type var implementation {$R *.DFM} type Dog := AnObject; // Doesen't compile procedure TDog.Bark; end. |
R.Anna
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)QuoteIn article <5kljgl$kp...@news10.gte.net>, mi...@gte.net wrote: Umm, I know you may have missed this part in your example, but I don't --=- Ritchie Annand |
Mike
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Conrad, Let me be a little more clear. I want to exploit the polymorphic nature To be more specific, I want to iterate through the nodes of a tree and Something like: with Node.Data as IExplorable do I am *so close* to making this very powerful mechanism work. Delphi's So, the crux of the matter is that at compile time I have no idea what So, how do you do that? Mike QuoteConrad Herrmann wrote: |
Ray Lischn
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)On 5 May 1997 21:29:57 GMT, mi...@gte.net wrote: Quote>type an interface), the interface must have a GUID, e.g., type Quote>var inherits from that static type of Dog. Quote> Dog := AnObject as IDog; // Doesen't compile QueryInterface. Quote> Dog := IDog(AnObject); // Doesen't compile AnObject implements the IDog interface. It will take some time to grow accustomed to interfaces. It's a new |
Mike
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Thank you for your helpful message. The part about needing a GUID I was Let me be a little more clear. I want to exploit the polymorphic nature To be more specific, I want to iterate through the nodes of a tree and Something like: with Node.Data as IExplorable do I am *so close* to making this very powerful mechanism work. Delphi's So, the crux of the matter is that at compile time I have no idea what So, how do you do that? Mike QuoteRay Lischner wrote: |
mi..
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Well, it looks as though I figured it out. Here it is: unit Unit1; interface uses type type type type var implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); // Downcast the Object to the base class that // Invoke the interface implemented methods procedure TPushable.PushMe; procedure TPushableSubclass.PushMe; end. |
Peter N Rot
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Mike <a...@gte.net> wrote in article <5kn4gs$dk...@news8.gte.net>... Quote> Let me be a little more clear. I want to exploit the polymorphic nature From the container (a tree in this case?), derive a container proc TFooTree.Add(const aFoo: TFoo); func TFooTree.ItemIs(const index: integer): TFoo; This gives you type safety at compile time, and the casts are all Provide a Display; virtual; for TFoo and all its derivatives. Finally (well, close to it), add a DisplayAll to TFooTree: var f: TFoo; or something like that. |
Conrad Herrman
![]() Delphi Developer |
Sat, 23 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)mi...@gte.net wrote in article <5knd4m$6n...@news2.gte.net>... Quote> Well, it looks as though I figured it out. interfaces, since you're not really using the interface to implement polymorphism, but instead using a virtual method. (Still, you do get other advantages from COM.) A demonstration of polymorphism through interfaces would include making a type TRock = class(TInterfacedObject, IPushable) Given an IUnknown pointer u, which you know points to either a door or a procedure PushTheObject( u: IUnknown); One difference between the TPushable/TPushableSubclass example and the -- Conrad Herrmann COM-based strangers meeting in Switzerland: |
Keith G. Murph
![]() Delphi Developer |
Sun, 24 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Cool. It's like multiple inheritance without dragging along unneeded Conrad Herrmann <cherrman-nojunkma...@ix.netcom.com> wrote in article Quote> mi...@gte.net wrote in article <5knd4m$6n...@news2.gte.net>... |
Conrad Herrman
![]() Delphi Developer |
Mon, 25 Oct 1999 03:00:00 GMT
Re:Casting Object as Interface (D3)Quote> Conrad Herrmann <cherrman-nojunkma...@ix.netcom.com> wrote in article <01bc5b01$b9353c60$656464c0@keithmur>... Quote> Cool. It's like multiple inheritance without dragging along unneeded implement interfaces, so there's obviously a similarity there. However, only in Mike's example is the actual code for Push shared. In my example, while both the TRock and TDoor can be pushed, each Push method has to be implemented separately. -- Conrad Herrmann |
1. Interfaces&Objects creation assignment compatible but not cast assignment compatible
3. why interface casting can burn in...
4. Casting a descendant class to an interface implemented by the base
5. error casting class to interface
6. Returning second interface pointer fromn the first interface in Automation object
8. HELP: nested objects and casting