formsclassdelphigenericsinherited

Delphi Form and Generics


I have a problem, see if you can help me. I have a base form.

type
  TForm_Base = class(TForm)
  oObjectoVO : TObject;
  ...
  procedure Search<M:class,constructor>;
  ...
  procedure TForm_Base.Search<M>;
  begin
    TBussinesObj<M>.Pesquisa(FDMemTableGrid);
  end;

And I have a form that inherits the base form.

procedure TForm_Client.FormCreate(Sender: TObject);
begin
  // TClient is class simple with the properties(write, read) of id, name, ...
  oObjectoVO := TClient.Create;
end;

procedure TForm_Client.ButtonSearchClick(Sender: TObject);
begin
  inherited;
end;

procedure TForm_Client.FormDestroy(Sender: TObject);
begin
  FreeAndNil(oObjectoVO);
end;

My problem is here. I cannot pass the type of the object instantiated in the client form, to the generic method (Search ) to the base form. I don't know if it's possible.

procedure TForm_Base.ButtonSearchClick(Sender: TObject);
begin
  Search<oObjectoVO.ClassType>; ******* Error *******
end;

Tanks.


Solution

  • Generics are a compile time construct. Consider this code:

    Search<oObjectoVO.ClassType>
    

    You are attempting to instantiate the generic with a type that is not known until run time.

    You need to change Search from being a generic to being non-generic and accepting a parameter that specifies the class.