0
votes

I have a TFrame that I use for searching for entities in a Delphi 2010 VCL project, in the TFrame I have a button edit, that allows the user to open a specific form to browse for that entity. (All the browse forms inherit from a common base browse form) Currently I achieve this by inheriting from the base frame, then implement the Browse event that fires off the specific form. The only difference each time is what form (type) is shown on the click event, is there a way I can achieve this with generics. That way I can reuse the same base frame without having to rewrite the same code for each entity (there are over 100), and at form create of the host form pass the type constraint to open the appropriate form on browse. I have tried adding a generic type to the frame:

type
  Browser<T: TfrmBrowser, constructor> = class
  class function BrowseForm(Owner: Tcomponent): T;
end;

class function Browser<T>.BrowseForm(Owner: Tcomponent): T;
var
_browseForm: T;
begin
  _browseForm := T.Create; // 1st problem T.Create(Owner); throws a comile error
  Result := _browseForm;
end;

and then in the picker frame I expose Start that can be called from the the host form's create event:

procedure TPickerFrame.Start<T>(const idProp, nameProp, anIniSection: string; aDto: IDto);
begin
    _browseForm:= Browser<T>.BrowseForm(self);
    _iniSectionName:= anIniSection;
    _idField:= idProp;
    _descriptionField:= nameProp;
    _dto := aDto;
end;

the truth is, I don't really get generics in Delphi, and none of this is working. Below are excerpts from the frame:

_browseForm: TfrmBrowser;

procedure TPickerFrame.Browse(var DS: TDataSet; var Txt: string; var mr: TModalResult);
begin
  // How do I achieve this with Generics
  // _browseForm := T.Create(nil); // <-- this line is what needs to know the form type at runtime
  // Everything else from here is the same
  _browseForm.ProductName := Application.Title;
  _browseForm.PageSize := 20;
  _browseForm.DatabaseType := bdbtADO;
  _browseForm.ADOConnection := dmdbWhereHouse.BaseADOConnection;
  _browseForm.INISectionName := _iniSectionName;
  _browseForm.DoSelBrowse(DS, Txt, mr, _descriptionField, _text);
  if mr = mrOk then
    begin
      DoSelect(DS);
    end;
end;

Does anyone have any experience with a similar requirement? Any help would be appreciated. Thanks

Below is an example of the rack master browser:

type
  TfrmMbfRACK_MASTER = class(TMxfrmBrowseHoster)
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    //...
  private
    FWHID: Integer;
    procedure SetWHID(const Value: Integer);
    { Private declarations }
  public
    { Public declarations }
    procedure BuildADO(Sender: TObject; Q: TADOQuery); override;
  end;

implementation

{$R *.DFM}

{ TfrmMbfRACK_MASTER }

procedure TfrmMbfRACK_MASTER.FormCreate(Sender: TObject);
  begin
    inherited;
    fmeMxFrmBrowseHoster1.KeyField := 'RACK_ID';
    // FWHID := -2; // 22/06/04
    FWHID := 0; // 22/06/04
  end;

procedure TfrmMbfRACK_MASTER.BuildADO(Sender: TObject; Q: TADOQuery);
  begin
    Q.Close;
    Q.SQL.Clear;
    Q.SQL.Add(
      'SELECT R.RACK_DESC, R.RACK_BARCODE, W.ERP_WH, WC.CLASS_NAME, W.DESCRIPTION WAREHOUSE, R.RACK_PACKING_ORDER,  ');
    //...
  end;

The base class

type
  TMxfrmBrowseHoster = class(TfrmMxForm)
  protected
    // ...
    procedure FormCreate(Sender: TObject);
    procedure BuildADO(Sender: TObject; ADOQ: TADOQuery); virtual; abstract;
  public

  procedure TMxfrmBrowseHoster.FormCreate(Sender: TObject);
  begin
    TMxFormProductName := Application.Title;
    fmeMxFrmBrowseHoster1.Initialise;
    INISectionName := Name;
    AbortAction := False;
    fmeMxFrmBrowseHoster1.OnSelect := SelectNormaliser;
    fmeMxFrmBrowseHoster1.OnNeedADO := BuildADO;
    fmeMxFrmBrowseHoster1.INISectionName := self.Name;
    fmeMxFrmBrowseHoster1.MultiSelect := dxBarLargeButton10.Down;
    fmeMxFrmBrowseHoster1.AutoSaveGrid := True;
    dxBarEdit1.OnChange := ActPageSizeChangedExecute;
    FormStorage.RestoreFormPlacement;

    ActConfirmDelete.Execute;
  end;
1
From what you posted it sounds like you don't really need generics, just polymorphic hierarchy of frames and forms.Ondrej Kelle
The issue is that TfrmBrowser is derived for each entity, so there's a TfrmBinBrowser = class(TfrmBrowser) and a TfrmRackBrowser, (up to 100 browsers), each with a different create event. So from a host form called frmPallets which needs to select a bin, we'd need to say fmeBinPicker.Start<TBinBrowser>('BIN_ID', 'BIN_NAME', ...);reckface
I tried TfrmBrowser(T).Create(Owner) already, and it simply called the create event of TfrmBrowser, not TFrmBinBrowserreckface
Thanks. The issue is this is a legacy project, I moved from Delphi 5 to 2010, and each of these derived forms do not implement a constructor, just the overridden FormCreate event.reckface

1 Answers

2
votes

I find your question a little on the vague side and I'm not 100% sure I understand exactly what you are asking. However, I know how to deal with your problem when calling the constructor. Perhaps that's all you need help with.

You need to use virtual constructor polymorphism and a bit of casting:

class function Browser<T>.BrowseForm(Owner: Tcomponent): T;
var
_browseForm: T;
begin
  _browseForm := TfrmBrowser(T).Create(Owner); 
  Result := _browseForm;
end;

This relies on virtual constructor polymorphism. So you must make sure that each constructor for every class derived from TfrmBrowser is marked with the override directive.