Question

I'm trying to implement MVC pattern in lazarus, I've created the model and the view (directly from the designer). Now, I'm writing the "controller", but when I try to re-assign the auto-generated method "Button.OnClick", I've a compilation error :

controller.pas(31,44) Error: Wrong number of parameters specified for call to "QuitBtn"

The constructor of the controller :

constructor TController.Create(AModel : TGame ; AView : TFArena);
begin
  Model := AModel;
  FView := AView;

  {Compilation Error}
  FView.Quit.OnClick := TController.QuitBtn;
end;

And the procedure :

procedure TController.QuitBtn(Sender : TObject);
begin
  ShowMessage('MVC READY');
end;       

Method auto-generated from lazarus

procedure TFArena.QuitClick(Sender: TObject);
begin
  {Nothing because she's gonna be over assigned}
end;   

So, the both avec the same parameter "Sender : TObject"

Thanks for reading

Was it helpful?

Solution

The OnClick event must be implemented a method of an instance. You've passed the code part of the method, but are missing the data, the instance.

You must get hold of an instance of TController. Now, you have one of those at hand since your code runs in the constructor of TController. So just write it like this:

FView.Quit.OnClick := Self.QuitBtn;

Of course, you can always omit Self which is idiomatic.

FView.Quit.OnClick := QuitBtn;

I'm also not sure that QuitBtn is a great name for an event handler. It sounds more like the name of a UI widget.

And perhaps you are using a compiler mode that requires the @ operator to generate method references:

FView.Quit.OnClick := @QuitBtn;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top