|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
For eachWhen 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. "cj" <cj@nospam.nospam> schrieb: I am curious why you are not using a 'For...To' loop.> 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. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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: Show quoteHide quote > "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. > "cj" <cj@nospam.nospam> schrieb: I would say it has been introduced to simplify looping through a collection > 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. 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/> 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 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. Show quoteHide quote "cj" <cj@nospam.nospam> wrote in message news:OzS%23G00KGHA.3856@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. >> Cj,
A for each loop will only work forwards. You will need to use a for next loop. Ken ------------------- Show quoteHide quote "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. > 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 Show quoteHide quote "Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message news:82F4B6F3-6999-43FD-BFC2-84CBF093BD17@microsoft.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. >>
Show quote
Hide quote
> The Proper way to code this would be to use a a Do Loop and control the Brrrrrrrrrrrrrrrr, proper for who, an assembler programmer, who learned less > 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 > or nothing from possibilities from higher languages. Cor 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 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 cj
> When I running a loop: For Each File As System.io.FileInfo In FileInfo Is Yes there is, however very inefficient to use, and because of the code bad > there a way to tell which file number (index) I'm currently on? to maintain, so probably only used by bad developpers who want to obfuscate there programs direct for everybody. http://msdn2.microsoft.com/system.collections.ienumerator.aspx The For Next Index gives all the information direct. I hope this helps, Cor 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: Show quoteHide quote > 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.
Windows Shell Integration: how?
Visual Basic 2005 CreateFile Read mail from exchange 2003 Problems with FileAccess and PcitoreBox Outlook Add-In: msoControlEdit: Textbox loses text when losing focus File is busy VB.NET - How do I set and keep data in the clipboard? convert text to access mdb Structured files in VS 2005 Dynamic Insert Statment |
|||||||||||||||||||||||