Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Anonymous Method

I would like to expose a function that can take an optional anonymous method :

    type
      TParamsProc = reference to procedure(Params: TSQLParams);
      TFieldsProc = reference to procedure(Fields: TSQLResult);

      TDbController = class
        ...
      public
        procedure Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
      end;

    implementation

    procedure TDbController.Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
    var
      Q: TUIBQuery;
    begin
      Q := TUIBQuery.Create(nil);
      try
        Q.Database := FDatabase;
        Q.Transaction := FTransaction;
        Q.SQL.Text := SQL;
        ParamsProc(Q.Params);
        Q.Open;
        while not Q.Eof do
        begin
          FieldsProc(Q.Result);
          Q.Next;
        end;
      finally
        Q.Free;
      end;
    end;

As sometimes I have no params to pass to a SQL Query, I would like to make the ParamsProc optional.

this code don't work :

      if ParamsProc <> nil then ParamsProc(Q.Params);

nor this one :

      if @ParamsProc <> nil then ParamsProc(Q.Params);

The first one don't compile, the second one compile but don't work because ParamsProc has always a non nil value.

Example of call :

      FController.Select(
        'select A, B, C from SOME_TABLE', 
        nil, 
        procedure(Fields: TSQLResult)
        begin
          FA := Fields.AsInteger[0];
          FB := Fields.AsString[1];
          FC := Fields.AsCurrency[2];
        end
      );

Edit

Seems that Assigned(ParamsProc) do the trick.

like image 334
ZeDalaye Avatar asked May 02 '11 10:05

ZeDalaye


People also ask

What are anonymous methods?

An anonymous method is a method which doesn't contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods.

What is true about anonymous method?

Anonymous methods are self-explanatory, implying "No Name" (anonymous) methods that let you declare a method body without giving it a name. Simply an anonymous method has only a body without a name, optional parameters and a return type. Anonymous methods can only be created when using delegates.

What are anonymous methods in Java?

It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overriding methods of a class or interface, without having to actually subclass a class.

What is the advantage of using anonymous methods?

The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.


1 Answers

Following Jeroen Pluimers advice, I make my "Edit" an "Answer" :

Assigned(ParamsProc) do the trick :

procedure TDbController.Select(const SQL: sting; ParamsProc: TParamsProc; FieldsProc: TFieldsProc);
var
  Q: TUIBQuery;
begin
  Q := TUIBQuery.Create(nil);
  try
    Q.Database := FDatabase;
    Q.Transaction := FTransaction;
    Q.SQL.Text := SQL;
    if Assigned(ParamsProc) then
      ParamsProc(Q.Params);
    Q.Open;
    while not Q.Eof do
    begin
      FieldsProc(Q.Result);
      Q.Next;
    end;
  finally
    Q.Free;
  end;
end;

Hope this helps !

like image 74
ZeDalaye Avatar answered Oct 05 '22 02:10

ZeDalaye