How do you get a reference to the TQuery object created in the WITH statement?

Possible duplicate:
The original instance of the object created using "c" in Delphi

One method that I use to create query objects in Delphi follows the first sample code. This gives me a reference to the object, and I can pass the object to a function.

procedure SomeProcedure;
var
  qry: TQuery;
begin
  qry := TQuery.Create(nil);

  with qry do
  begin
    Connection := MyConn;
    SQL.Text := 'SELECT * FROM PEOPLE';
    Open;

    funcDisplayDataSet(qry);
    Free;
  end;

end;

Can this also be done in the WITH statement, where your Create object is contained in the WITH statement?

procedure SomeProcedure;
begin
  with TQuery.Create(nil) do
  begin
    Connection := MyConn;
    SQL.Text := 'SELECT * FROM PEOPLE';
    Open;

    funcDisplayDataSet( ??? );  // Here I'm unsure how to pass the object created...
    Free;
  end;
end;

Can I pass this dynamic object to a function like `funcDisplayDataSet (TQuery)?

I just wanted to know if this is possible. I am not looking for a summary of why the WITH clause is bad or good. There are other posts in the StackOver thread with this discussion. *

+5
source share
3

, , . :

  • , Self.
  • with .

1 , . . , ? , , RRUZ, . .

2. , .

+6

?

type
  TQueryHelper = class helper for TQuery
  public
    function Instance: TQuery;
  end;


function TQueryHelper.Instance: TQuery;
begin
  Result := Self;
end;

   With TQuery.Create(nil) do
   begin
     SQL.Text:='Select * from OTGH';
     ShowMessage(Instance.SQL.Text);
   end;
+5

The operator withdoes not provide a link that you can use to transmit outside the instructions.

With help, TQueryyou can bypass it using a property that has a reference to the parent:

begin
  with TQuery.Create(nil) do
  begin
    Connection := MyConn;
    SQL.Text := 'SELECT * FROM PEOPLE';
    Open;
    // Fields have reference to parent component, but your implementation may vary
    funcDisplayDataSet(TQuery(Fields.Fields[0].GetParentComponent));
    Free;
  end;
end;
+3
source

All Articles