By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

For each

P: n/a
cj
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.
Feb 6 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"cj" <cj@nospam.nospam> schrieb:
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.


I am curious why you are not using a 'For...To' loop.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Feb 6 '06 #2

P: n/a
cj
Curiosity is a good thing. Right? I think so. Initially there was no
need for the functionality I'm requesting now and For Each is IMHO
specifically made for looping through collections/arrays so I used it.

So, why don't I change to "For To" etc now? Well, I will if I have to.
I am just curious if I could do this in For Each.

Hope someone knows.
Herfried K. Wagner [MVP] wrote:
"cj" <cj@nospam.nospam> schrieb:
When I running a loop: For Each File As System.io.FileInfo In
FileInfo Is there a way to tell which file number (index) I'm
currently on? Actually I'd like the ability to skip to a particular
index--either forward or back. It'd be nice to be able to set the
index back or forward with a command button perhaps and the loop just
move to that position and continue forward from there.

I'm specifically interested in doing this in the for each loop.


I am curious why you are not using a 'For...To' loop.

Feb 6 '06 #3

P: n/a
"cj" <cj@nospam.nospam> schrieb:
Curiosity is a good thing. Right? I think so. Initially there was no
need for the functionality I'm requesting now and For Each is IMHO
specifically made for looping through collections/arrays so I used it.


I would say it has been introduced to simplify looping through a collection
or array when index-based access is not important. Obviously the latter is
important in the case you described...

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 6 '06 #4

P: n/a
Cj,

A for each loop will only work forwards. You will need to use a for
next loop.

Ken
-------------------
"cj" wrote:
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.

Feb 6 '06 #5

P: n/a
Hi cj,

Thanks for posting!

As Ken and Herfried mentioned, the "For Each" statement is not like the
"For" loop statement. Since "For Each" statement is only working forward,
there is no need to supply the functionalilty of the index. In this
scenario, I suggest you use the "For" loop statement instead of. I
appreciate your understanding.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support

Feb 7 '06 #6

P: n/a
CMM
Um, "IndexOf" function perhaps? Arrays have this as do all the collections
AFAIK.

Though I would agree with what the others recommed as IndexOf would probably
slow down your loop and is sorta dumb to use if you can just change your
look to For x.. To.
"cj" <cj@nospam.nospam> wrote in message
news:Oz****************@TK2MSFTNGP12.phx.gbl...
Curiosity is a good thing. Right? I think so. Initially there was no
need for the functionality I'm requesting now and For Each is IMHO
specifically made for looping through collections/arrays so I used it.

So, why don't I change to "For To" etc now? Well, I will if I have to. I
am just curious if I could do this in For Each.

Hope someone knows.
Herfried K. Wagner [MVP] wrote:
"cj" <cj@nospam.nospam> schrieb:
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.


I am curious why you are not using a 'For...To' loop.

Feb 7 '06 #7

P: n/a
CMM
The Proper way to code this would be to use a a Do Loop and control the
increment manually.

Do Until whatever_condition

'do whatever work on
'fileInfo(i)

If whatever Then
i += 1
Else whatever2 then
i -= 3
...
End If
Loop
"Ken Tucker [MVP]" <Ke**********@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Cj,

A for each loop will only work forwards. You will need to use a
for
next loop.

Ken
-------------------
"cj" wrote:
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.

Feb 7 '06 #8

P: n/a
cj
When I running a loop: For Each File As System.io.FileInfo In FileInfo Is
there a way to tell which file number (index) I'm currently on?


Yes there is, however very inefficient to use, and because of the code bad
to maintain, so probably only used by bad developpers who want to obfuscate
there programs direct for everybody.

http://msdn2.microsoft.com/system.co...numerator.aspx

The For Next Index gives all the information direct.

I hope this helps,

Cor
Feb 7 '06 #9

P: n/a
> The Proper way to code this would be to use a a Do Loop and control the
increment manually.

Do Until whatever_condition

'do whatever work on
'fileInfo(i)

If whatever Then
i += 1
Else whatever2 then
i -= 3
...
End If
Loop

Brrrrrrrrrrrrrrrr, proper for who, an assembler programmer, who learned less
or nothing from possibilities from higher languages.

Cor
Feb 7 '06 #10

P: n/a
CMM
Did you even read the poster's question?
Feb 7 '06 #11

P: n/a
CMM,

Yes even in Cobol does this give bad code (high change on errors). However
Cobol is in fact not event driven and therefore has less the problem from
recursive behaviour.

(I have the idea that cj asks this from a Cobol background).

Cor
Feb 7 '06 #12

P: n/a
CMM,.

You deserve it that I explain this Cobol part a little bit more, know that
it is for me as well very long back in time.

In Cobol is the instruction "Perform", an instruction I never saw back in
any other language. It is extremely powerfull.

You can handle with it an endless amount of tables wich can be endless deep
inside tables as well. Those can be referenced by direct pointers however as
well by referenced pointers. It is no problem to set them conditional up and
down whenever you want.

However you can more with it, you can as well perform performs inside a
perform, although you can do the same with a for in a for, is it with a
"perform" shorter to write.

Maybe I have for cj direct answers his problem with this.

Cor
Feb 7 '06 #13

P: n/a
cj
I had a lot of posts to this question after I left work yesterday so
I'll make one blanket reply here. Thanks! To everyone. Seems there is
a way with ienumerator (thanks Cor). But that does sound too
complicated for me to get into. At least I was not overlooking
something obvious.

I couldn't find any way to get the current index while in the "For Each"
loop. I'd thought of Indexof too and it should return the index of the
item I'm on. (Although if you have several identical items in a
collection that'd throw a wrench into that. For me if I used the file
name which would be logical it should be fine as there wouldn't be
duplicate file names.) However indexof will not allow me to reposition
the for each loop to a new index.

I'd envisioned something like the windows media player's progress bar.
In my program jpgs are flipped by one by one and the progress bar shows
how far through the available jpgs it' gone. In WMP I can click on the
progress bar and have it instantly jump to that place in the
song/movie/whatever and it jumps--forward or back--to that point and
continues playing forward from there.

I understood from the beginning this could be done in a "For To" loop.
And, it's absolutely no problem to change the program to "For To" loop.
I just wanted to know if I was overlooking some functionality in "For
Each" loop.

So, I changed to "For To" and now am looking at determining what index
to reposition to based on a click on a progress bar--see the post I made
4 hours after this one. Gary has made an interesting suggestion I think
I'll play around with.


cj wrote:
When I running a loop: For Each File As System.io.FileInfo In FileInfo
Is there a way to tell which file number (index) I'm currently on?
Actually I'd like the ability to skip to a particular index--either
forward or back. It'd be nice to be able to set the index back or
forward with a command button perhaps and the loop just move to that
position and continue forward from there.

I'm specifically interested in doing this in the for each loop.

Feb 7 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.