Utilisation d'une classe de base

Résolu
Utilisateur anonyme - 11 mai 2009 à 13:57
 Utilisateur anonyme - 12 mai 2009 à 21:45
Salut à tous,

J'ai une petite question à vous soumettre :  J'ai fais un composant basé sur ce principe

 TCustomCollectionItem = class(TCollectionItem)
  private { properties }
    fTime:Single;
   fDoubleTime:Single;
  published { properties }
    Property Time:Single Read fTime Write fTime;
    Property DoubleTime:Single Read fDoubleTime Write fDoubleTime;
  end;

   TA=Class(TCustomCollectionItem )
   Private
      fPenColor:TColor;
  published
      Property PenColor:TColor Read fPenColor Write fPenColor;
   End;

   TB=Class(TCustomCollectionItem )
   Private
      fGrid:TPicture;
  published
      Property Grid:TColor Read fGrid Write fGrid;
   End;

   TC=Class(TCustomCollectionItem )
   Private
      fLabel:TLabel;
  published
      Property Label:TLabel Read fLabel Write fLabel;
   End;

   TFinal=class(TComponent)
   Private
     fA:TA;
     fB:TB;
     fC:TC;
   Published
     Property  A:TA Read fA Write fA;
     Property B:TB Read fB Write fB;
     Property C:TC Read fC Write fC;
   End;

  Actuellement j'utilise ce composant ainsi

     Final.A.Time:=15;
     Final.B.Time:=30;
     Final.C.Time:=45;

 Mais je trouve ca moche . Je me demandais s'il y avait pas un moyen de faire une boucle, un truc du genre :
Final.TCustomCollectionItem(Index).Time:=15*(Index+1);. Si quelqu'un a une idée (Même si je dois retoucher pas mal de chose), je suis prenneur.

Merci .

5 réponses

f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
12 mai 2009 à 14:32
type
  TItemType = (itBase, itA, itB, itC);
 
  TCustomCollectionItem = class(TCollectionItem)
  private
    fItemType   : TItemType;
    fTime       : Single;
    fDoubleTime : Single;
  protected 
    function GetDisplayName : string; override;
  published
    property ItemType   : TItemType read fItemType;
    Property Time       : Single    read fTime       write fTime;
    Property DoubleTime : Single    read fDoubleTime write fDoubleTime;
  public 
    constructor Create(Collection : TCollection); override;
    procedure Assign(Source : TPersistent); override;
  end;

  TA=Class(TCustomCollectionItem)
  private
    fPenColor: TColor;
  protected 
    function GetDisplayName : string; override;
  published
    property PenColor: TColor Read fPenColor Write fPenColor;
  public 
    constructor Create(Collection : TCollection); override;
    procedure Assign(Source : TPersistent); override;
  end;

  TB=Class(TCustomCollectionItem)
  private
    fGrid: TPicture;
  protected 
    function GetDisplayName : string; override;
  published
    Property Grid: TPicture Read fGrid Write fGrid;
  public 
    constructor Create(Collection : TCollection); override;
    procedure Assign(Source : TPersistent); override;
  end;

  TC=Class(TCustomCollectionItem)
  private
    fLabel: TLabel;
  protected 
    function GetDisplayName : string; override;
  published
    property _Label: TLabel Read fLabel Write fLabel;
  public 
    constructor Create(Collection : TCollection); override;
    procedure Assign(Source : TPersistent); override;
  end;

  TFinal=class(TComponent)
  private
    fABC : array[0..2] of TCustomCollectionItem;
    function GetAsTA(const index: integer): TA;
    function GetAsTB(const index: integer): TB;
    function GetAsTC(const index: integer): TC;
    function GetItem(index: integer): TCustomCollectionItem;
    procedure SetAsTA(const Index: Integer; const Value: TA);
    procedure SetAsTB(const Index: Integer; const Value: TB);
    procedure SetAsTC(const Index: Integer; const Value: TC);
    procedure SetItem(index: integer; const Value: TCustomCollectionItem);
  public
    procedure SetAllTime(const TmA, TmB, TmC: single);
    procedure SetAllSameTime(const TmABC: single);
    procedure SetAllDoubleTime(const DblTmA, DblTmB, DblTmC: single);
    procedure SetAllSameDoubleTime(const DblTmABC: single);
  published
    property Items[index: integer]: TCustomCollectionItem read GetItem write SetItem;
    Property A: TA index 0 read GetAsTA write SetAsTA;
    Property B: TB index 1 read GetAsTB write SetAsTB;
    Property C: TC index 2 read GetAsTC write SetAsTC;
  end;

{ TFinal }

function TFinal.GetAsTA(const index: integer): TA;
begin
  result := TA(fABC[index]);
end;

function TFinal.GetAsTB(const index: integer): TB;
begin
  result := TB(fABC[index]);
end;

function TFinal.GetAsTC(const index: integer): TC;
begin
  result := TC(fABC[index]);
end;

function TFinal.GetItem(index: integer): TCustomCollectionItem;
begin
  result := fABC[Index];
end;

procedure TFinal.SetAllDoubleTime(const DblTmA, DblTmB, DblTmC: single);
begin
  if Assigned(fABC[0]) then fABC[0].DoubleTime := DblTmA;
  if Assigned(fABC[1]) then fABC[1].DoubleTime := DblTmB;
  if Assigned(fABC[2]) then fABC[2].DoubleTime := DblTmC;
end;

procedure TFinal.SetAllSameDoubleTime(const DblTmABC: single);
begin
  if Assigned(fABC[0]) then fABC[0].DoubleTime := DblTmABC;
  if Assigned(fABC[1]) then fABC[1].DoubleTime := DblTmABC;
  if Assigned(fABC[2]) then fABC[2].DoubleTime := DblTmABC;
end;

procedure TFinal.SetAllSameTime(const TmABC: single);
begin
  if Assigned(fABC[0]) then fABC[0].Time := TmABC;
  if Assigned(fABC[1]) then fABC[1].Time := TmABC;
  if Assigned(fABC[2]) then fABC[2].Time := TmABC;
end;

procedure TFinal.SetAllTime(const TmA, TmB, TmC: single);
begin
  if Assigned(fABC[0]) then fABC[0].Time := TmA;
  if Assigned(fABC[1]) then fABC[1].Time := TmB;
  if Assigned(fABC[2]) then fABC[2].Time := TmC;
end;

procedure TFinal.SetAsTA(const Index: Integer; const Value: TA);
begin
  fABC[index] := Value;
end;

procedure TFinal.SetAsTB(const Index: Integer; const Value: TB);
begin
  fABC[index] := Value;
end;

procedure TFinal.SetAsTC(const Index: Integer; const Value: TC);
begin
  fABC[index] := Value;
end;

procedure TFinal.SetItem(index: integer; const Value: TCustomCollectionItem);
begin
  fABC[index] := Value;
end;

{ TCustomCollectionItem }

procedure TCustomCollectionItem.Assign(Source: TPersistent);
begin
  if (source is TCustomCollectionItem) then
    with TCustomCollectionItem(Source) do
    begin
      Self.fTime       := fTime;
      Self.fDoubleTime := fDoubleTime;
      Self.fItemType   := fItemType;
    end
  else
    inherited Assign(source);
end;

constructor TCustomCollectionItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  fTime       := 0;
  fDoubleTime := 0;
  fItemType   := itBase;
end;

function TCustomCollectionItem.GetDisplayName: string;
begin
  result := '<'+IntToStr(Self.Index)+'>';
end;

{ TA }

procedure TA.Assign(Source: TPersistent);
begin
  if (source is TA) then
  begin
    fPenColor := TA(source).fPenColor;
    inherited assign(TCustomCollectionItem(Source));
  end
  else
    inherited Assign(source);
end;

constructor TA.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  fPenColor := clBlack;
  fItemType := itA;
end;

function TA.GetDisplayName: string;
begin
  result := inherited GetDisplayName+' type A';
end;

{ TB }

procedure TB.Assign(Source: TPersistent);
begin
  if (source is TB) then
  begin
    fGrid := TB(source).fGrid;
    inherited assign(TCustomCollectionItem(Source));
  end
  else
    inherited Assign(source);
end;

constructor TB.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  fGrid := nil;
  fItemType := itB;
end;

function TB.GetDisplayName: string;
begin
  result := inherited GetDisplayName+' type B';
end;

{ TC }

procedure TC.Assign(Source: TPersistent);
begin
  if (source is TC) then
  begin
    fLabel := TC(source).fLabel;
    inherited Assign(TCustomCollectionItem(Source));
  end
  else
    inherited Assign(source);
end;

constructor TC.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  fLabel := nil;
  fItemType := itC;
end;

function TC.GetDisplayName: string;
begin
  result := inherited GetDisplayName+' type C';
end;

<hr size="2" width="100%" />
3
Guillemouze Messages postés 991 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 29 août 2013 6
12 mai 2009 à 14:41
const

  ID_A = 0;

  ID_B = 1;

  ID_C = 2;

type
TFinal=class(TComponent)
   Private
    fItems: array[ID_A..ID_C] of
TCustomCollectionItem ;
    function GetItem(Index: integer): TCustomCollectionItem; // Result := fItems[Index];
    procedure SetItem(Index: integer; const Value: TCustomCollectionItem ); // fItems[Index] := Value
   Public
     Property Item[Index: integer]: TCustomCollectionItem Read GetItem Write SetItem;
   Published
     Property  A: TCustomCollectionItem index ID_A Read GetItem Write SetItem;
     Property  B: TCustomCollectionItem index ID_B Read GetItem Write SetItem;
     Property  C: TCustomCollectionItem index ID_C Read GetItem Write SetItem;

   End;

Final.Item[ID_A].Time := 15;
0
Utilisateur anonyme
12 mai 2009 à 20:48
Merci à vous deux .

@Guillemouze : J'ai pensé à ta méthode mais en fait elle ne répond pas tout à fait à ma problématique . En effet il faut que A soit déclaré comme un TA et non un TCustomCollectionItem (Idem pour B et C). En fait je cherche à utiliser dans un premier uniquement les propriétés de A, B et C  relatives à TCustomCollectionItem. Puis apres (mais c'est la seconde étape et elle a lieu dans une autre procédure ) utilisér les propriétés spécifiques à A ou à B ou à  C.

@f0xi : ca répond à ma demande. Ceci dit j'ai une question : Ai je besoin de définir le create dans TCustomCollectionItem dans la mesure ou dans TA un create est définit (Idem pour TB et TC) et qu'il ne s'agit pas d'un vrai héritage dans ce cas (enfin je me comprend).

En tout cas encore merci
0
f0xi Messages postés 4205 Date d'inscription samedi 16 octobre 2004 Statut Modérateur Dernière intervention 12 mars 2022 35
12 mai 2009 à 21:14
moi je me demande surtout pourquoi tu as utilisé TCollectionItem alors que tu n'utilise pas de TCollection, et que tu n'ai pas penser a utiliser TPersistent a la place puisque placé dans un TComponent...

<hr size="2" width="100%" />
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Utilisateur anonyme
12 mai 2009 à 21:45
@f0xi : A mais si si, j'utilise TCollection : je te rassure .

En fait je l'avais souligné au tout début de ma question : il s'agit juste du principe.  Ca n'a rien de confidentiel, seulement le composant fait 1500 lignes : j'ai préféré vous épargner une séance de spéléologie (surtout qu'il y a des implications de collection) et rentrer dans le vif du sujet. Maintenant effectivement présenté comme ca, je peux comprendre que cela peut paratre stupide
0
Rejoignez-nous