Board index » delphi » VB "For Each" in Delphi

VB "For Each" in Delphi

How do i get the sam behavior that "for each" does in VB in delphi..
that is, loops trough a collection and gets every item in the collection

--
/Benny

 

Re:VB "For Each" in Delphi


If you want a foreach tLabel:

for i := 0 to Object.ComponentCount do
    if Object.Component[i] is tLabel then (*... etc*)

John Bleau

Re:VB "For Each" in Delphi


In article <3ce24b5f_2@dnews>, Benny S <benny.sk...@daladatorer.se>
writes
Quote
>How do i get the sam behavior that "for each" does in VB in delphi..
>that is, loops trough a collection and gets every item in the collection

>--
>/Benny

type  TmysetParentRange = ....

var myset : set of TmysetParentRange;
    i     : TmysetParentRange;

...
for i:=low(TmysetParentRange) to high(TmysetParentRange) do
  if i in myset then
    ....

Bernard Hill
Braeburn Software
Author of Music Publisher system
Music Software written by musicians for musicians
http://www.braeburn.co.uk
Selkirk, Scotland
01750 21854
+44 1750 21854

Re:VB "For Each" in Delphi


Nah..not quite that i'm after...
Got a Com -object that has some kinda collection for some tablenames that I
want to extract..
But mayb something similar will work... But I doubt it has a
ComponentCount..then what?
--
/Benny

"John Bleau" <jrbleauNOS...@sympatico.ca> skrev i meddelandet
news:3ce2559a$1_2@dnews...

Quote
> If you want a foreach tLabel:

> for i := 0 to Object.ComponentCount do
>     if Object.Component[i] is tLabel then (*... etc*)

> John Bleau

Re:VB "For Each" in Delphi


On Wed, 15 May 2002 16:01:11 +0200, "Benny S"

Quote
<benny.sk...@daladatorer.se> wrote:
>some kinda collection

What kind of collection exactly?

Nick Hodges
Lemanix Corporation
How to ask questions of techies --
http://www.tuxedo.org/~esr/faqs/smart-questions.html

Re:VB "For Each" in Delphi


Quote
> Nah..not quite that i'm after...
> Got a Com -object that has some kinda collection for some tablenames
that I
> want to extract..
> But mayb something similar will work... But I doubt it has a
> ComponentCount..then what?

COM collections are objects that have a method with DispId -4 usually
called _NewEnum that returns an IUnknown or an enumeration interface,
probably IEnumVariant (in your case it could be IEnumString as well).
So, see if your object has a _NewEnum method or a subobject that
represents the collection (usually its name will be in plural, like
"items") that has a _NewEnum method and do this:

var
  Enum: IEnumVariant;
  Fetched: Cardinal;
  Elt: OleVariant;
begin
  Enum := Obj.Items._NewEnum as IEnumVariant;
  while Enum.Next(1, Elt, Fetched) = S_OK do
  begin
    // In each iteration Elt contains an element of the collection
  end;
end;

Re:VB "For Each" in Delphi


Quote
>Got a Com -object that has some kinda collection for some tablenames that I

most COM Collections have a Count property. You just have to remember
that they also usually start referencing items from 1 not 0 as we do
in object pascal.

ex:
  for lcv := 1 to db.Tables.Count do begin

Dennis Passmore
Ultimate Software, Inc

"If you cannot conceive the idea you
 will never achieve the desired results"

Re:VB "For Each" in Delphi


Quote
> most COM Collections have a Count property. You just have to
remember
> that they also usually start referencing items from 1 not 0 as we do
> in object pascal.

> ex:
>   for lcv := 1 to db.Tables.Count do begin

But most COM collection implementors are optimized for VB's FOR EACH
that uses _NewEnum so if you iterate them using Count and Item() they
are a lot slower. VSS automation objects are a very good example of
this behaviour. I always use the _ForEach method just to be sure.

Other Threads