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.  

C++ Examples: 

 

/*
  This example saves params from a ClientDataSet, alters them
  and restores them.
*/
TParams *SavedParams;

void __fastcall TForm1::SaveParamsClick(TObject *Sender)
{
  // Save the parameters for the TDataSet
  SavedParams->Assign(CDS->Params);
}

void __fastcall TForm1::AlterParamsClick(TObject *Sender)
{
  CDS->Params->CreateParam(ftInteger, "StateParam", ptInput);
  CDS->Params->CreateParam(ftInteger, "MyParam", ptInput);
  for (int i = 0; i < CDS->Params->Count; i++)
    if ((CDS->Params->Items[i]->IsNull) &&
       (CDS->Params->Items[i]->DataType == ftInteger))
      // Items is the default property, so you can omit its name
      CDS->Params->Items[i]->AsInteger = -1;
}

void __fastcall TForm1::RestoreParamsClick(TObject *Sender)
{
  // Restore the parameters to TDataSet
  CDS->Params->AssignValues(SavedParams);
}

#include <memory>       //for STL auto_ptr class

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Initialize SavedParams
  static std::auto_ptr<TParams> _SavedParamsCleaner(SavedParams = new TParams());
}

 

Delphi Examples: 

{
  This example saves params from a ClientDataSet, alters them
  and restores them.
}
var SavedParams: TParams;

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) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!