Board index » delphi » VB "for each" in Delphi

VB "for each" in Delphi

Hi:
  I'm trying to use the MSAgent objects but
the VB Examples use some thing like this:

For Each Animation in Genie.AnimationNames
      Genie.Play Animation
   Next

so the question is how to simulate the for each
in Delphi

Thanks

Victor Gimeno

 

Re:VB "for each" in Delphi


Binh Ly has written classes that simulate a foreach.

But I guess something like like :

Y:=Genie.AnimationNames.count
for i:=1 to Y do
  Genie.Play (Genie.AnimationNames[i])

should work...

--
Arthur Hoornweg
(To answer me, please remove the ".net" from my e-mail address)

Re:VB "for each" in Delphi


Quote
----- Original Message -----
From: Arthur Hoornweg <arthur.hoorn...@planet.nl.net>

> Y:=Genie.AnimationNames.count
> for i:=1 to Y do
>   Genie.Play (Genie.AnimationNames[i])

> should work...

Just out of curiosity, is this any more efficient than:

 for i:=1 to Genie.AnimationNames.count do
   Genie.Play (Genie.AnimationNames[i])

I only ask because I was stepping through some code the other day where the
upper limit in the for loop was returned by a function - I noticed that the
function was only called once to obtain the "to" limit for the loop, not on
each iteration of the loop.  What is more, the result of the function was
actually subject to a "Pred()" as well.

I realise that if it is no more or less efficient then there is nothing to
be gained from not declaring a temp limit variable (i.e. such as "Y") other
than a little less typing, although I would also argue that the direct use
of the "Count" property in the loop declaration makes it less likely that
the purpose of the loop could be confused.

As I said, just some idle curiosity.

--
Jolyon Smith
Neot Software Services Ltd.
http://www.neot.co.uk

Re:VB "for each" in Delphi


Most implementers of IEnumVARIANT (foreach) also customarily implement array
indexing like you say below. However, this is not always the case so
sometimes you have no option but wrestle IEnumVARIANT or use an IEnumVARIANT
wrapper like my TEnumVariant class.

have fun
--
Binh Ly
http://www.techvanguards.com

Quote
"Arthur Hoornweg" <arthur.hoorn...@planet.nl.net> wrote in message

news:8l681i$dh85@bornews.borland.com...
Quote
> Binh Ly has written classes that simulate a foreach.

> But I guess something like like :

> Y:=Genie.AnimationNames.count
> for i:=1 to Y do
>   Genie.Play (Genie.AnimationNames[i])

> should work...

> --
> Arthur Hoornweg
> (To answer me, please remove the ".net" from my e-mail address)

Re:VB "for each" in Delphi


Quote
"Jolyon Smith" <jsm...@neot.co.uk> wrote in message news:3976af63@dnews...
> ----- Original Message -----
> From: Arthur Hoornweg <arthur.hoorn...@planet.nl.net>

> > Y:=Genie.AnimationNames.count
> > for i:=1 to Y do
> >   Genie.Play (Genie.AnimationNames[i])

> > should work...

> Just out of curiosity, is this any more efficient than:

>  for i:=1 to Genie.AnimationNames.count do
>    Genie.Play (Genie.AnimationNames[i])

> I only ask because I was stepping through some code the other day where
the
> upper limit in the for loop was returned by a function - I noticed that
the
> function was only called once to obtain the "to" limit for the loop, not
on
> each iteration of the loop.  What is more, the result of the function was
> actually subject to a "Pred()" as well.

This is a Delphi-specific for-loop optimization, although this feature might
not be available in other client environments. Therefore, in Delphi, there
is no performance difference.

have fun
--
Binh Ly
http://www.techvanguards.com

Re:VB "for each" in Delphi


Yes, I've noticed that too. Moreover, some automation objects
in MSWord implement an "item" property where the
documentation states that the default property of the object should
behave like an array.

The following (late-binding) example should officially work, but it doesn't:

      DOC:=Wordapp.ActiveDocument;
      Range:=Doc.Range;
      font:=Range.Paragraphs[1].Range.Font;

Delphi gives an error in line 3 saying that the object has no "items"
property.
The MSWORD VBA documentation, however, says that "paragraphs" has
a default array property.

Instead:      font:=Range.Paragraphs.item(1).Range.Font;
works fine.

BTW, Microsoft always indexes array properties as one-based. Delphi
programmers customarily do it zero-based. If I implement array properties
in an object (with or without ienumvariant), should I use the one or the
other?

--
Arthur Hoornweg
(To answer me, please remove the ".net" from my e-mail address)

Binh Ly <b...@castle.net> schrieb in im Newsbeitrag: 3977c739@dnews...

Quote
> Most implementers of IEnumVARIANT (foreach) also customarily implement
array
> indexing like you say below. However, this is not always the case so
> sometimes you have no option but wrestle IEnumVARIANT or use an
IEnumVARIANT
> wrapper like my TEnumVariant class.

> have fun
> --
> Binh Ly
> http://www.techvanguards.com

> "Arthur Hoornweg" <arthur.hoorn...@planet.nl.net> wrote in message
> news:8l681i$dh85@bornews.borland.com...
> > Binh Ly has written classes that simulate a foreach.

> > But I guess something like like :

> > Y:=Genie.AnimationNames.count
> > for i:=1 to Y do
> >   Genie.Play (Genie.AnimationNames[i])

> > should work...

> > --
> > Arthur Hoornweg
> > (To answer me, please remove the ".net" from my e-mail address)

Re:VB "for each" in Delphi


Thanks all

Victor Gimeno

Re:VB "for each" in Delphi


Quote
"Arthur Hoornweg" <arthur.hoorn...@planet.nl.net> wrote in message

news:8l8pt0$3ac5@bornews.borland.com...
Quote
> BTW, Microsoft always indexes array properties as one-based. Delphi
> programmers customarily do it zero-based. If I implement array properties
> in an object (with or without ienumvariant), should I use the one or the
> other?

I haven't seen this documented anywhere. Anyway, the Automation convention
seems to be 1-based so I would stick with 1-based collections.

have fun
--
Binh Ly
http://www.techvanguards.com

Re:VB "for each" in Delphi


Quote
> I haven't seen this documented anywhere. Anyway, the Automation convention
> seems to be 1-based so I would stick with 1-based collections.

That's rather unpleasant. I happen to THINK zero-based...

--
Arthur Hoornweg
(To answer me, please remove the ".net" from my e-mail address)

Other Threads