Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interface instance...

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

I've built an interface which is implemented in all of my forms. This works great and do exaclty what I want to:

type TfrmTest = class(TForm, IFormIntf)}

Also, I succeeded to create dynamically a form and again, this works perfectly:

var frm: IFormIntf
frm := TfrmTest.Create(Application);

The problem I have now is to bring back the instance of the interface to its original creator and invoke a specific method not included in the interface (I also tried to call and overload method without results):

(frm as TfrmTest).SpecificMethod;

Any ideas?

Thanks in advance,

Rej Cloutier
 
I'm pretty sure you can't do this, which makes sense as it would defeat the point of interfaces in the first place.
The idea of an interface is to create a level of abstraction from your code, so that your code needs only to know what methods and properties are offered by the interface and doesn't care about the class that actually implements this.
In your example you create frm as an instance of TfrmTest, in which case you might just as well declare it as type TfrmTest anyway and then you will have access to all methods of TfrmTest! A 'proper' use of interfaces would be something like this:

Code:
var
  frm: IFormIntf;
begin
  frm := CoFormIntf.Create;
  frm.MyInterfaceMethod;
end;

where CoFormIntf is the CoClass used to create instances of FormIntf. In this case the code wouldn't know or care if IFormIntf was implemented by TfrmTest or TfrmTest2 or anything else.
Hope that made some sense

Steve
 
Hi Steve,

Finally got it!

I've implemented a function in my interface with a TForm result type in which the only code is:

function ReturnTForm(): TForm
begin
Result := (Self as TForm);
end;

Using this new function it's now possible to return to the original interface creator and call a specific method:

(frm.ReturnTForm as TfrmTest).SpecificMethod

Thank you

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top