RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCollection.Assign Method

Copies the contents of another collection to the object where the method is executed.

Pascal
procedure Assign(Source: TPersistent); override;
C++
virtual __fastcall Assign(TPersistent * Source);

Use Assign to copy the contents of one TCollection instance to another. The Assign method deletes all items from the destination collection (the object where it is executed), then adds a copy of each item in the source collection's Items array. 

Source is another object (typically another collection) that contains the items that replace this collection's items.  

Delphi Examples: 

 

var SavedParams: TParams;
{ Initialize SavedParams }
SavedParams := TParams.Create;
try
  { Save the parameters for SQLDataSet1 }
  SavedParams.Assign(SQLDataSet1.Params);
  { Do something with SQLDataSet1 }
  ...
  { Restore the parameters to SQLDataSet1 }
  SQLDataSet1.Params.AssignValues(SavedParams);
finally
  SavedParams.Free;
end; 

 

{
  This example saves params from a ClientDataSet, alters them
  and restores them.
}
procedure TForm1.SaveParamsClick(Sender: TObject);
begin
  { Save the parameters for the TDataSet }
  SavedParams.Assign(CDS.Params);
end;

procedure TForm1.AlterParamsClick(Sender: TObject);
var
  I : Integer;
begin
  CDS.Params.CreateParam(ftInteger, 'StateParam', ptInput);
  CDS.Params.CreateParam(ftInteger, 'MyParam', ptInput);
  for I := 0 to CDS.Params.Count - 1 do
    if (CDS.Params.Items[I].IsNull) and
       (CDS.Params.Items[I].DataType = ftInteger) then
      { Items is the default property, so you can omit its name }
      CDS.Params[I].AsInteger := -1;
end;

procedure TForm1.RestoreParamsClick(Sender: TObject);
begin
  { Restore the parameters to TDataSet }
  CDS.Params.AssignValues(SavedParams);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  { Initialize SavedParams }
  SavedParams := TParams.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SavedParams.Free;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!