Discussion:
Weird WITH statement problem
(too old to reply)
Dunric
2003-12-09 15:35:32 UTC
Permalink
Let:
Global variable is defined in main program scope (f.E.: Delphi project
file).
Variable of record type is defined in the same scope. Name of one of
the record elements is equal to a name of this global variable.

Question:
How to correctly reference global variable within with..do statement
when its name conflicts with name of element defined in a record ?
Maybe it's trivial, but I could't find any solution :-(

sample code of Test.dpr:
----------------------------
program test;
{$APPTYPE GUI}
uses Windows, Messages;


type
MyRecord = record
myInst: LongWord;
someOther: Boolean;
end;

var
myInst: LongWord;
myRec: MyRecord;

...

begin
...
myInst := HInstance;
with myRec do
begin
myInst := {global} myInst; // !!!!!!!!!!!!!!!!!!!!!
someOther := False;
end;
...
end.

When variable is defined in a delphi unit there is no problem(point
operator) but in a project file ?
Chris Mantoulidis
2003-12-10 18:50:37 UTC
Permalink
Well, to be exact, it shouldn't work in none so I guess you're lucky.

You've got 2 variables with the same name, and the one's in a record.
So when you do "with <...> do" you have 2 variables with the same name
and the program doesn't know which to choose.

Solution: rename either variable OR do not use the with statement
CBFalconer
2003-12-10 19:30:23 UTC
Permalink
Post by Chris Mantoulidis
You've got 2 variables with the same name, and the one's in a record.
So when you do "with <...> do" you have 2 variables with the same name
and the program doesn't know which to choose.
Yes it does. The one accessed via the with statement is the
closest enclosing scope, and takes precedence, thus hiding the
other variable entirely.

I believe you have already been told not to use the
comp.lang.pascal group, as it has long been superseded. FUP set
to comp.lang.pascal.misc
--
Chuck F (***@yahoo.com) (***@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Hanford Carr <"handycarr"@comcast.net>
2003-12-12 02:13:15 UTC
Permalink
Post by Dunric
Global variable is defined in main program scope (f.E.: Delphi project
file).
Variable of record type is defined in the same scope. Name of one of
the record elements is equal to a name of this global variable.
How to correctly reference global variable within with..do statement
when its name conflicts with name of element defined in a record ?
Maybe it's trivial, but I could't find any solution :-(
----------------------------
program test;
{$APPTYPE GUI}
uses Windows, Messages;
type
MyRecord = record
myInst: LongWord;
someOther: Boolean;
end;
var
myInst: LongWord;
myRec: MyRecord;
...
begin
...
myInst := HInstance;
with myRec do
begin
myInst := {global} myInst; // !!!!!!!!!!!!!!!!!!!!!
someOther := False;
end;
...
end.
When variable is defined in a delphi unit there is no problem(point
operator) but in a project file ?
with myRec do
begin
myInst := test.myInst; { Use the Program ID }
someOther := False;
end;

Simply use the program ID and a dot. It works in BP7 so I don't see why
it wouldn't work in Delphi.

Cheers Hanford

Loading...