variable object in "with object do ..."

Hi,

is there a possibility to use a variable in the  "with object do  ..." construct  for the 'object' ?

Thanks,
KPBecker

KPBeckerAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
see above

procedure TFormClassName.WithParametersDo(const AParameters: TParameters);
begin
  If assigned(AParameters) then
  with AParameters do
  begin
     ParamByName('pE').Value:= MAPInfo[12];
     ParamByName('pA').Value:= MAPInfo[13];
     ParamByName('pS').Value:= MAPInfo[14];
     ParamByName('pK').Value:= ABGInfo[4];
     ParamByName('pB').Value:= MAPInfo[15];
     ParamByName('pM').Value:= MAPInfo[16];
     .....
  end;
end;


......
if whatever then
  withParametersdo (queryA.Parameters)
else
  withParametersdo (queryB.Parameters);
.....

or maybe also

var p : TParameters
......
  if whatever then
    p := queryA.Parameters
  else
    p := queryB.Parameters;
  with p do
  begin
     ParamByName('pE').Value:= MAPInfo[12];
     ParamByName('pA').Value:= MAPInfo[13];
     ParamByName('pS').Value:= MAPInfo[14];
     ParamByName('pK').Value:= ABGInfo[4];
     ParamByName('pB').Value:= MAPInfo[15];
     ParamByName('pM').Value:= MAPInfo[16];
  end;
  ......


just from head, means not tested and typos possible

meikl ;-)





 

0
 
Russell LibbySoftware Engineer, Advisory Commented:
Yes, as long as the variable is a structure (record).

eg:

TPerson = record
 age: integer;
 name: string;
end;

var
 person: TPerson;

begin

 with person do
 begin
  age:=20;
  name:='John';
 end;


-------------

Regards,
Russell
0
 
kretzschmarCommented:
maybe also

procedure WithAnyObjectOfaSpecificClassDo(const AObject : TYourClassYouHave);
begin
  if assigned(AObject) then
  with AObject do
  begin
    .....
  end;
end;

meikl ;-)
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
kretzschmarCommented:
btw.

WithAnyObjectOfaSpecificClassDo

should be better named as

WithAnyInstanceOfaSpecificClassDo

but of course its just a phantasy-name

meikl ;-)
0
 
robert_marquardtCommented:
With creates nothing but an anonymous variable which is local to the block of the with statement.
0
 
Russell LibbySoftware Engineer, Advisory Commented:

I took the question to mean "other than an object instance", thus providing the record example. But all of the above info is correct.
From the Delphi help:

A with statement is a shorthand for referencing the fields of a record or the fields, properties, and methods of an object. The syntax of a with statement is

with obj do statement

or

with obj1, ..., objn do statement

where obj is a variable reference denoting an object or record, and statement is any simple or structured statement. Within statement, you can refer to fields, properties, and methods of obj using their identifiers alone—without qualifiers.
For example, given the declarations

type TDate = record

  Day: Integer;
  Month: Integer;
  Year: Integer;
end;

var OrderDate: TDate;

you could write the following with statement.

with OrderDate do

  if Month = 12 then
  begin
    Month := 1;
    Year := Year + 1;
  end
  else
    Month := Month + 1;

This is equivalent to

if OrderDate.Month = 12 then

begin
  OrderDate.Month := 1;
  OrderDate.Year := OrderDate.Year + 1;
end
else
  OrderDate.Month := OrderDate.Month + 1;

If the interpretation of obj involves indexing arrays or dereferencing pointers, these actions are performed once, before statement is executed. This makes with statements efficient as well as concise. It also means that assignments to a variable within statement cannot affect the interpretation of obj during the current execution of the with statement.
Each variable reference or method name in a with statement is interpreted, if possible, as a member of the specified object or record. If there is another variable or method of the same name that you want to access from the with statement, you need to prepend it with a qualifier, as in the following example.

with OrderDate do

  begin
    Year := Unit1.Year
    ...
  end;

When multiple objects or records appear after with, the entire statement is treated like a series of nested with statements. Thus

with obj1, obj2, ..., objn do statement

is equivalent to

with obj1 do

  with obj2 do
     ...
     with objn do
       statement

In this case, each variable reference or method name in statement is interpreted, if possible, as a member of objn; otherwise it is interpreted, if possible, as a member of objn–1; and so forth. The same rule applies to interpreting the objs themselves, so that, for instance, if objn is a member of both obj1 and obj2, it is interpreted as obj2.objn.


---------------

Russell

0
 
kretzschmarCommented:
well russell, good explaination, but

let say

type To1 = Class(TObject)
                    f1 : AnyType;
                 end;


var
  fo1, fo2 : To1;
begin
  ....creating
  with fo1, fo2 do
     f1 := f1;  //what happened
  end;    
end;

or

var
  fo1, fo2 : To1;
begin
  ....creating
  with fo1 do
    with fo2 do
       f1 := f1;  //what happened
    end;
  end;    
end;

currently i'm avoid with ... do
its just for lazy coders (from my point of view)
and makes the code less readable

meikl ;-)
0
 
Russell LibbySoftware Engineer, Advisory Commented:
Blame it on Borland (their doc, not mine...)


;-)
Russell

0
 
BlackTigerXCommented:
"with" considered harmful... =o)
0
 
Russell LibbySoftware Engineer, Advisory Commented:
"With" is useful as long as it's nuances are understood, and as long as one avoids ambiguous situations (such as that provided by Meikl). Another common mistake is to forget about the "Self" in an object method, eg:

// Label1 is of type TLabel
procedure TForm1.Something;
begin

 // Intention is to assign the form caption to the label caption
  with Label1 do
    Caption:=Caption;

end;

But would in effect be the same as

 Label1.Caption:=Label1.Caption;

As in Meikl's case, the variable just got assigned its own f1 field. Probably not what was intended. So in short, there are some gotcha's to it. When in doubt its probably best to skip the shorthand coding style.

my 2cents
Russell



0
 
BlackTigerXCommented:
how about:

  with Label1 do
    Caption:=Name

...with can really be evil
0
 
robert_marquardtCommented:
with can be *very* useful for ActiveX/COM.
If you have a COM object which is very deeply nested (many dereferencing dots a.b.c.d) then it gives you a performance boost.
The dereferencing is very costly for COM objects so a local variable helps. I got a tenfold boost once.

Of course a local variable is as good, but with is elegant.
0
 
KPBeckerAuthor Commented:
Hi,

reading the comments given above I think I should have been a bit more specific with my question.

Depending on "whatever" a lot of SQL-statements using "queryA" or "queryB" have to be performed

if whatever then
  queryA.Parameters.ParamByName('pE').Value:= MAPInfo[12];
  queryA.Parameters.ParamByName('pA').Value:= MAPInfo[13];
  queryA.Parameters.ParamByName('pS').Value:= MAPInfo[14];
  queryA.Parameters.ParamByName('pK').Value:= ABGInfo[4];
  queryA.Parameters.ParamByName('pB').Value:= MAPInfo[15];
  queryA.Parameters.ParamByName('pM').Value:= MAPInfo[16];
  ...
else
  queryB.Parameters.ParamByName('pE').Value:= MAPInfo[12];
  queryB.Parameters.ParamByName('pA').Value:= MAPInfo[13];
  queryB.Parameters.ParamByName('pS').Value:= MAPInfo[14];
  queryB.Parameters.ParamByName('pK').Value:= ABGInfo[4];
  queryB.Parameters.ParamByName('pB').Value:= MAPInfo[15];
  queryB.Parameters.ParamByName('pM').Value:= MAPInfo[16];
  ...
end;


What I would like to do is to use a    "with  query.Parameters  do  begin ..."
where  "query.Parameters"  is set to  "queryA.Parameters"  or "queryB.Parameters"
depending on "whatever"

KPBecker
0
 
KPBeckerAuthor Commented:
Hello, meikl,

that's what I was looking for, thanks.

KPBecker
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.