Search code examples
delphidelphi-2010

Create a Procedure using a TClientDataset as a parameter


I want to create a procedure in Delphi 2010.

This procedure will receive a TClientDataset 'cdTable1' parameter and an X Integer and it has to perform an action of close/open dataset:

if cdTable1.Active then cdTable1.Close; cdTable1.Params.ParamByName('MyParameter').AsString:=X; cdTable1.Open;

How can I define it and run it?


Solution

  • Try this:

    procedure TForm1.MyProcName(cds : TClientDataSet; X : Integer);
    begin
        if cds.Active then 
            cds.Close; 
        cds.Params.ParamByName('MyParameter').AsString := X; 
        cds.Open;
    end;
    
    procedure TForm1.Button1Click(Sender : TObject);
    begin
        MyProcName(cdTabel1, 1234);
    end;
    

    In the interface section, where TForm1 is defined, you have to add:

    public
        procedure MyProcName(cds : TClientDataSet; X : Integer);
    

    If you are using a TDataModule, probably procedure MyProcName would find a nice place there instead of the form.