?
Solved

variable object in    "with  object  do ..."

Posted on 2005-04-21
14
Medium Priority
?
321 Views
Last Modified: 2013-11-23
Hi,

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

Thanks,
KPBecker

0
Comment
Question by:KPBecker
  • 4
  • 4
  • 2
  • +2
14 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13834483
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13835523
maybe also

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

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13835545
btw.

WithAnyObjectOfaSpecificClassDo

should be better named as

WithAnyInstanceOfaSpecificClassDo

but of course its just a phantasy-name

meikl ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:robert_marquardt
ID: 13836021
With creates nothing but an anonymous variable which is local to the block of the with statement.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13836085

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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 13836165
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
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13836228
Blame it on Borland (their doc, not mine...)


;-)
Russell

0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13836252
"with" considered harmful... =o)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 13836360
"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
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13836419
how about:

  with Label1 do
    Caption:=Name

...with can really be evil
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 13840145
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
 

Author Comment

by:KPBecker
ID: 13840241
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 500 total points
ID: 13840386
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
 

Author Comment

by:KPBecker
ID: 13944368
Hello, meikl,

that's what I was looking for, thanks.

KPBecker
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question