Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What's the proper way to use a for-in loop with a TObjectList? (re: Delphi 2006)

463 views
Skip to first unread message

John Klimek

unread,
Feb 27, 2007, 11:22:30 AM2/27/07
to

I'm trying to use a simple for-in loop with a TObjectList but it says that it expects a pointer as the iterator variable.

For example:

var
Obj : TObject;
begin
for Obj in MyObjectList do
Obj.DoSomething();

The compiler says that Obj should be a pointer, so should I use the following code: (??)

var
Obj : ^TLogger;
begin
for Obj in MyObjectList do
Obj.DoSomething();

The above code works but since objects are really just pointers why wouldn't my first section of code work?

What is the "proper" way of iterating through a TObjectList using a for-in loop?

Thanks,
John

Craig Stuntz [TeamB]

unread,
Feb 27, 2007, 11:37:37 AM2/27/07
to
John Klimek wrote:

> The above code works but since objects are really just pointers why
> wouldn't my first section of code work?
>
> What is the "proper" way of iterating through a TObjectList using a
> for-in loop?

for..in works with TObjectList because of TList.GetEnumerator, which
returns a Pointer. The code you have is as good as it's going to get
for now; at least your pointer is typed!

The situation will improve when Delphi for Win32 gets generics.

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz
How to ask questions the smart way:
http://www.catb.org/~esr/faqs/smart-questions.html

John Klimek

unread,
Feb 27, 2007, 12:48:47 PM2/27/07
to

Actually, my code throws an access violation.

This appears to be the only way to get it to work:

var
_Logger : Pointer;
begin
for _Logger in FLoggers do
TLogger(_Logger).Log(LogItem);
end;

Is that normal?

Craig Stuntz [TeamB]

unread,
Feb 27, 2007, 1:04:25 PM2/27/07
to
John Klimek wrote:

> Is that normal?

It is normal to have to cast stuff returned as an untyped pointer,
yes.

--
Craig Stuntz [TeamB] · Vertex Systems Corp. · Columbus, OH
Delphi/InterBase Weblog : http://blogs.teamb.com/craigstuntz

IB 6 versions prior to 6.0.1.6 are pre-release and may corrupt
your DBs! Open Edition users, get 6.0.1.6 from http://mers.com

Rob Kennedy

unread,
Feb 27, 2007, 7:23:13 PM2/27/07
to
Craig Stuntz [TeamB] wrote:
> for..in works with TObjectList because of TList.GetEnumerator, which
> returns a Pointer. The code you have is as good as it's going to get
> for now; at least your pointer is typed!
>
> The situation will improve when Delphi for Win32 gets generics.

QC bug 10790, which deals with this TObjectList issue, is marked as
deferred until the next release. I guess that means Delphi 2007, but as
I understand it, Delphi 2007 won't change the interface of any classes.
Fixing this problem would require introducing TObjectList.GetEnumerator
to replace TList.GetEnumerator.

--
Rob

Marc Rohloff [TeamB]

unread,
Feb 27, 2007, 8:37:25 PM2/27/07
to
On 27 Feb 2007 08:22:30 -0800, John Klimek wrote:

> The compiler says that Obj should be a pointer, so should I use the following code: (??)

Does it work if you do:

var
Obj : TLogger;
begin
for pointer(Obj) in MyObjectList do
Obj.DoSomething();

?

--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com

0 new messages