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
> 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
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?
> 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
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
> 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