Einzelnen Beitrag anzeigen

Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.352 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: Generics-Typparameter einer TObjectlist herausfinden

  Alt 18. Mär 2012, 11:03
Meinst du vielleicht sowas?
Delphi-Quellcode:
unit Unit33;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections, TypInfo, Vcl.Grids;

type
  TMyTestStringGridHelper = class helper for TStringGrid
  public
    procedure DisplayList<T: class>(AList: TObjectList<T>);
  end;

  TTest = class(TObject)
  private
    FValue: Integer;
    procedure SetValue(const Value: Integer);
  public
    constructor Create(const AValue: Integer);
    function ToString: string; override;
    property Value: Integer read FValue write SetValue;
  end;

  TForm33 = class(TForm)
    StringGrid1: TStringGrid;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form33: TForm33;

implementation

{$R *.dfm}

procedure TForm33.FormCreate(Sender: TObject);
var
  a: TObjectList<TTest>;
  i: Integer;
begin
  a := TObjectList<TTest>.Create(True);
  try
    for i := 0 to 10 do
      a.Add(TTest.Create(i));
    StringGrid1.DisplayList<TTest>(a);
  finally
    a.Free;
  end;
end;

{ TMyTestStringGridHelper }

procedure TMyTestStringGridHelper.DisplayList<T>(AList: TObjectList<T>);
var
  i: Integer;
begin
  RowCount := AList.Count;
  for i := 0 to AList.Count - 1 do
    Cells[0, i] := AList[i].ToString;
end;

{ TTest }

function TTest.ToString: string;
begin
  Result := IntToStr(FValue);
end;

constructor TTest.Create(const AValue: Integer);
begin
  FValue := AValue;
end;

procedure TTest.SetValue(const Value: Integer);
begin
  FValue := Value;
end;

end.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat