Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forming a variable name with string + integer and calling functions

Tags:

delphi

Firstly, I am new to these and the question could be stupid. Anyway, I have a procedure like this:

procedure Tform1.QueryChange(sqltext : String; query : Integer);
begin
if query = 1 then begin
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add(sqltext);
ADOQuery1.Open;
end;
if query = 2 then begin
ADOQuery2.Close;
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add(sqltext);
ADOQuery2.Open;
end;

I would like to remove the if blocks and make one united code:

ADOQuery+query.Close; (know that looks very silly)
ADOQuery+query.SQL.Clear;
ADOQuery+query.SQL.Add(sqltext);
ADOQuery+query.Open;

My goal is when query=1 code will use ADOQuery1.Close; etc. when query=2 code will use ADOQuery2.Close;

like image 524
emrey Avatar asked Dec 02 '22 23:12

emrey


1 Answers

You could create a local variable that referred to the TADOQuery object that you wish to operate on. Like this:

var
  ADOQuery: TADOQuery;
begin
  if query=1 then
    ADOQuery := ADOQuery1
  else if query=2 then
    ADOQuery := ADOQuery2;
  ADOQuery.Close;
  ADOQuery.SQL.Clear;
  ADOQuery.SQL.Add(sqltext);
  ADOQuery.Open;      
end;
like image 164
David Heffernan Avatar answered May 27 '23 20:05

David Heffernan