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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem when passing a TForm subclass implementing interface

Status
Not open for further replies.

jslarochelle

Programmer
Jun 30, 2005
2
US
I'm a Java programmer trying to write a little application in DELPHI (version 4).

I have a form that implements an interface:

Code:
  { Here's the interface (simplified)}
  IUserInterface = interface
     procedure reportError (errorMessage: string);
  end;

  { Here are the pertinent parts of the form definition}
  TMainForm = class(TForm, IUserInterface)
     { Stuff generated by the form designer...}

     public
        procedure reportError (errMsg: string);
        procedure otherMethod;

     {etc...}
  end;

In the implementation I have:

Code:
        procedure TMainForm.reportError (errMsg: string);
        begin {...}

Everything compiles until I try to pass MainForm (an instance of TMainForm) to a method of another object that expects a IUserInterface. The signature of the method is something like:

Code:
   procedure doSomething (var userInterf: IUserInterface);

I'm doing this in TMainForm.otherMethod;

I always get an error message saying that there is no overloaded method that can be used with these arguments. I use MainForm an instance of TMainForm declared in the interface of the unit.

Anyone as any idea why I get that error ?
 
The only thing i see here is that you use different name for the parameter (which you pass to reportError) in the interface and in the MainForm.

The procedure within the TMainForm shold look like this:

procedure reportError (errorMessage: string);

Other that that i can't say anything else.

I hope this helps.
 
Sorry jslarochelle.

It seems that i replied too soon. I didn't read your post and think. :)

Now that i'm thinking of it, i think you cannot send an instance of TMainForm as an interfaced object.

If you do need some info from the MainForm within the doSomething, i suggest you either change the type of the variable of use the handle of the MainForm.

Remember that you can always call the IUserInterface.reportError from the MainForm. I'm not entirely sure what you're trying to do so i'm just throwing some ideas.
 
Thanks for your suggestions.

I did think that there was something special about TForm subclasses that was causing the problem because I have other implementations of IUserInterface that do work. It would be nice to have an explanation though because the class does compile (until I try to use it as a parameter). I think that a more meaningfull message from the compiler would be in order or preferably a message when the interface is added to TMainForm.

What I will probably do is create another implementation of IUserInterface (in the TMainForm unit) that will delegate the actual work to TMainForm.

The idea is to have a business class use an abstraction instead of having a dependency on the gui class.

Thanks again for your comment.
JS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top