Recent

Author Topic: variable type of constructor / passing constructor as parameter  (Read 3459 times)

yogo1212

  • New Member
  • *
  • Posts: 22
variable type of constructor / passing constructor as parameter
« on: October 23, 2013, 10:58:20 am »
Hi,

I'm stuck storing a constructor in a variable to avoid circular unit-dependencies. Those could be avoided by using correct subclasses, but that would break the purpose of event-driven-programming.

This is the constructor i want to pass:
Code: [Select]
//Unit TaskForm

TTaskForm = class(TForm)
constructor CreateNew(TheOwner: TComponent; ntinfo: TTaskInfo); 

This is the class it is being passed to:

Code: [Select]
//Unit TaskPanel
TTaskFormConstructor = function(TheOwner: TComponent; ntinfo: TTaskInfo): TForm of object;

{ TTaskPanel }

TTaskPanel = class(TPanel)
constructor Create(TheOwner: TComponent; ntinfo: TTaskInfo;
nparentFormConstr: TTaskFormConstructor);

This is the assignment containing the assignment in question:
Code: [Select]
tmppanel := TTaskPanel.Create(Self, tmptask, TTaskFormConstructor(@TTaskForm.CreateNew) );


Yields this error:
Code: [Select]
taskform.pas(125,47) Error: Illegal type conversion: "<address of function(TComponent,TTaskInfo):TTaskForm of object;Register>" to "<procedure variable type of function(TComponent,TTaskInfo):TForm of object;Register>"
I want to avoid re-ordering the classes, because the inherited classes make use of the various TForm-Events - like OnMouseMove and such - and when those are reused in subclasses, my original handlers would be overriden.

kind regards, yogo1212

#Edit: changed names for clarity
« Last Edit: October 23, 2013, 11:03:26 am by yogo1212 »

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: variable type of constructor / passing constructor as parameter
« Reply #1 on: October 23, 2013, 12:55:16 pm »
The error message states that TForm is not TTaskForm. Method calls needs to be identical. If you run into trouble with your architecture you could think about an abstract class as basis.
Lazarus 1.7 (SVN) FPC 3.0.0

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 11443
  • Debugger - SynEdit - and more
    • wiki
Re: variable type of constructor / passing constructor as parameter
« Reply #2 on: October 23, 2013, 01:18:43 pm »
pass the class, instead of the constructor:
Code: [Select]
  TTaskFormClass = class of TTaskForm;

You need a virtual constructor
(and an overwritten one, in inherited classes)
Code: [Select]
TTaskForm = class(TForm)
constructor CreateNew(TheOwner: TComponent; ntinfo: TTaskInfo); VIRTUAL;


Then in your  code
Code: [Select]
TTaskPanel = class(TPanel)
constructor Create(TheOwner: TComponent; ntinfo: TTaskInfo;
nparentFormClass: TTaskFormClass );

you can call
Code: [Select]
nparentFormClass.Create

yogo1212

  • New Member
  • *
  • Posts: 22
Re: variable type of constructor / passing constructor as parameter
« Reply #3 on: October 23, 2013, 02:45:27 pm »
Thank you very much!!
This got me started:

pass the class, instead of the constructor:
Code: [Select]
  TTaskFormClass = class of TTaskForm;


Thank you for providing sample code as well.



#Edit: How do i mark this as resolved?
« Last Edit: October 23, 2013, 02:47:39 pm by yogo1212 »

 

TinyPortal © 2005-2018