Board index » delphi » Object and TCollectionItem

Object and TCollectionItem

I've got an object A=class(TPersistant). How can I use this object as a
TCollectionItem?

I like to show it in the collection from TGXBaseDataset.

Herbert

 

Re:Object and TCollectionItem


Quote
In article <3aa0005c_1@dnews>, Herbert Schlup wrote:
> I've got an object A=class(TPersistant). How can I use this object as a
> TCollectionItem?

You cannot directly, an item that can be put into a TCollection has to
derive from TCollectionItem, not TPersistent. But you can write a
TCollectionItem descendent that "wraps" your class. It would store an
instance of your class in an internal field an surface the properties of
that internal object as properties of its own. Needs a bunch of Get and
Set methods that "redirect" access to the collectionitems properties to
the inner objects properties.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Object and TCollectionItem


Peter

Thanx for your advice. But i have really no idea, how i can do that. Do you
have any sample?

Herbert

Re:Object and TCollectionItem


Try this sort of thing.

Cheers,
Jim Cooper

interface

type
  TMyObject = class(TPersistent)
  private
    FMyProp : string;
  public
    property MyProp : string read FMyProp write FMyProp;
  end;

  TMyCollectionItem = class(TCollectionItem)
  private
    FMyObject : TMyObject;
    function GetMyProp : string;
    procedure SetMyProp(Value : string);
  public
    constructor Create(Collection : TCollection); override;
    destructor Destroy; overide;

    property MyProp : string read GetMyProp write SetMyProp;
  end;

implementation

constructor TMyCollectionItem.Create(Collection : TCollection);
begin
  inherited;
  FMyObject := TMyObject.Create;
end;

destructor TMyCollectionItem.Destroy;
begin
  FMyObject.Free;
  inherited;
end;

function TMyCollectionItem.GetMyProp : string;
begin
  Result := '';

  if Assigned(FMyObject) then begin
    Result := FMyObject.MyProp;
  end;
end;

procedure TMyCollectionItem.SetMyProp(Value : string);
begin
  if Assigned(FMyObject) then begin
    FMyObject.MyProp := Value;
  end;
end;

_____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Writing Palm conduits with Delphi
_____________________________________________

Re:Object and TCollectionItem


Quote
In article <3aa132fc_2@dnews>, Herbert Schlup wrote:
> Thanx for your advice. But i have really no idea, how i can do that. Do you
> have any sample?

See Jims reply, he has a perfect example of what i meant.
Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Re:Object and TCollectionItem


Thank you for your reply. But here are my concerns...

So I really have to create for every TMyObject Class a corresponding
TMyCollectionItem Class????

Phuuu.... not really maintainable... or?

Herbert

Re:Object and TCollectionItem


Quote
> > So I really have to create for every TMyObject Class a corresponding
> > TMyCollectionItem Class????

> If you want to use collections, yes. There is no alternative. Of

I often find that I inherit from TCollectionItem rather than
TPersistent, then no wrapping is required. For properties of components
I put the items in a descendant of TOwnedCollection so that they stream
automatically, and I get a editor for the items, and so on.

Cheers,
Jim Cooper

_____________________________________________

Jim Cooper      jcoo...@tabdee.ltd.uk
Tabdee Ltd      http://www.tabdee.ltd.uk

TurboSync - Writing Palm conduits with Delphi
_____________________________________________

Other Threads