Newsgroup: it.comp.lang.delphi
Titolo: Collection & Component

Ciao a tutti, 
sto cercando di scrivere un componente custom 
che abbia fra le property una Collection, tipo
la property FIELDS di un TSQLQUERY o simili.
Ho preso spunto da questo articolo : 
http://www.howtodothings.com/viewarticle.aspx?article=383
ma non riesco a reperire il valore di un item.
Il programma si pianta, e dopo un bel pò va in overflow.
Di seguito il codice : 
unit myCollection;
interface
uses
  SysUtils, Classes, Controls;
type
  TOurCollectionItem = class(TCollectionItem)
  private
    FSomeValue : String;
  protected
    function GetDisplayName : String; override;
  public
    procedure Assign(Source: TPersistent); override;
  published
     property SomeValue : String read FSomeValue write FSomeValue;
  end;
  TOurCollection = class(TOwnedCollection)
  private
    FOwner : TComponent;
    FItems : TList;
    FItemClass: TCollectionItemClass;
  protected
    function GetItem(Index: Integer): TOurCollectionItem;
    procedure SetItem(Index: Integer; Value: TOurCollectionItem);
    procedure Update(Item: TOurCollectionItem);
  public
    function Add : TOurCollectionItem;
    function Insert(Index: Integer): TOurCollectionItem;
    property ItemClass: TCollectionItemClass read FItemClass;
    property Items[Index: Integer]: TOurCollectionItem read GetItem
write SetItem;
  end;
TCollectionComponent = class(TComponent)
  private
    FOurCollection : TOurCollection;
    procedure SetOurCollection(const Value:
      TOurCollection);
  public
    constructor Create(AOwner : TComponent); override;
    destructor Destroy; override;
  published
    property OurCollection : TOurCollection
      read FOurCollection
      write SetOurCollection;
  end;
procedure Register;
implementation
procedure Register;
begin
  RegisterComponents('TestComp', [TCollectionComponent]);
end;
procedure TOurCollectionItem.Assign(Source: TPersistent);
begin
  if Source is TOurCollectionItem then
    SomeValue := TOurCollectionItem(Source).SomeValue
  else
    inherited; //raises an exception
end;
function TOurCollectionItem.GetDisplayName: String;
begin
  Result := Format('Item %d',[Index]);
end;
function TOurCollection.Add: TOurCollectionItem;
begin
  Result := TOurCollectionItem.Create(Self);
  Added(TCollectionItem(Result));
end;
function TOurCollection.GetItem(Index: Integer): TOurCollectionItem;
begin
  Result := FItems[Index];
end;
procedure TOurCollection.SetItem(Index: Integer; Value: TOurCollectionItem);
begin
   TOurCollection(FItems[Index]).Assign(Value);
end;
procedure TOurCollection.Update(Item: TOurCollectionItem);
begin
end;
function TOurCollection.Insert(Index: Integer): TOurCollectionItem;
begin
  Result := Add;
  Result.Index := Index;
end;
constructor TCollectionComponent.Create(AOwner: TComponent);
begin
  inherited;
  FOurCollection := TOurCollection.Create(Self);
end;
destructor TCollectionComponent.Destroy;
begin
  FOurCollection.Free;
  inherited;
end;
procedure TCollectionComponent.SetOurCollection(
  const Value: TOurCollection);
begin
  FOurCollection.Assign(Value);
end;
end.
Ho qualche dubbio sui metodi GetItem / SetItem .....
Ciao 
Rino

Rino
	Ciao
	MassimoB
 
---------------------------------
http://www.massimobeschi.it/
---------------------------------
Se tu pagare come dici tu ...
Io lavorare come dico io ...
Se tu pagare come dico io ...
Io lavorare come dici tu ...
---------------------------------