Search code examples
delphihelperclass-helpers

Extending class from another unit


We can extend any class so:

TColumn = class(FMX.Grid.TColumn)
private
  FId: Integer;
public 
  property Id: Integer read FId write FId;
end;

And this is works fine in our own unit, but if we try this (Grid: TGrid on the form):

procedure ChangeId;
var
  Col: TColumn;
begin
  Col := Grid.Columns[0];
  Col.Id := 0;
end;

We got error:

[dcc32 Error] uHelpers.pas(136): E2010 Incompatible types:
'uHelpers.TColumn' and 'FMX.Grid.TColumn'

Any way to extend FMX.Grid.TColumn class to make procedure ChangeId correct?


Solution

  • That is possible, but we should to add columns manually at runtime:

    TIdColumn = class(FMX.Grid.TColumn)
    private
      FId: Integer;
    public 
      property Id: Integer read FId write FId;
    end;
    
    Grid.AddObject(TIdColumn.Create(Grid));
    
    procedure ChangeId;
    var
      Col: TIdColumn;
    begin
      Col := Grid.Columns[0] as TIdColumn;
      Col.Id := 0;
    end;