Link to home
Start Free TrialLog in
Avatar of KPBecker
KPBecker

asked on

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

Avatar of Russell Libby
Russell Libby
Flag of United States of America image

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
maybe also

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

meikl ;-)
btw.

WithAnyObjectOfaSpecificClassDo

should be better named as

WithAnyInstanceOfaSpecificClassDo

but of course its just a phantasy-name

meikl ;-)
Avatar of robert_marquardt
robert_marquardt

With creates nothing but an anonymous variable which is local to the block of the with statement.

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

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 ;-)
Blame it on Borland (their doc, not mine...)


;-)
Russell

"with" considered harmful... =o)
"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



how about:

  with Label1 do
    Caption:=Name

...with can really be evil
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.
Avatar of KPBecker

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, meikl,

that's what I was looking for, thanks.

KPBecker