Board index » delphi » Getting object back from interface

Getting object back from interface

I have an interface method which accepts an Interface object as a parameter

TAccount = class ( TAutoObject, IAccount )
TPayment  = class( TAutoObject, IPayment )

procedure TAccount.AddPayment( Payment : IPayment );

Inside the above procedure, I would like to translate Payment to a TPayment
to process it.  How do I do this.  I know that you can simply use TPayment
as an IPayment when you need the interface from an object, but the reverse
is not true.

Any help would be greatly appreciated.

 

Re:Getting object back from interface


The way I do it is to implement an integer handle property in IAccount (Or
IPayment) and have this property return integer(self). You can later (in
AddPayment) do something like:

ThePaymentObject := TPayment(Payment.handle);

Ron.

Quote
Eric Johnson wrote in message <01bd7cf6$141c25f0$0300a8c0@eric>...
>I have an interface method which accepts an Interface object as a parameter

>TAccount = class ( TAutoObject, IAccount )
>TPayment  = class( TAutoObject, IPayment )

>procedure TAccount.AddPayment( Payment : IPayment );

>Inside the above procedure, I would like to translate Payment to a TPayment
>to process it.  How do I do this.  I know that you can simply use TPayment
>as an IPayment when you need the interface from an object, but the reverse
>is not true.

>Any help would be greatly appreciated.

Re:Getting object back from interface


In article <6j7i07$8...@forums.borland.com>, rlo...@hyperact.com says...

Quote
> The way I do it is to implement an integer handle property in IAccount (Or
> IPayment) and have this property return integer(self). You can later (in
> AddPayment) do something like:

> ThePaymentObject := TPayment(Payment.handle);

> Eric Johnson wrote in message <01bd7cf6$141c25f0$0300a8c0@eric>...
> >I have an interface method which accepts an Interface object as a parameter

> >TAccount = class ( TAutoObject, IAccount )
> >TPayment  = class( TAutoObject, IPayment )

> >procedure TAccount.AddPayment( Payment : IPayment );

> >Inside the above procedure, I would like to translate Payment to a TPayment
> >to process it.  How do I do this.  I know that you can simply use TPayment
> >as an IPayment when you need the interface from an object, but the reverse
> >is not true.

This will only work if the objects are in the same process.  If you
really want to do this, you'll have to write a method to recreate the
object in the client process based on information in the interface.  COM
isn't really built to do this, so this is an unusual approach.  Why don't
you just expand the functionality of the interface so you don't have to
have the whole object?

--Dylan

--
Dylan Steinberg (stein...@iscg.com)
Lead Consultant
Integrated Systems Consulting Group

Other Threads