For each

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cj

    For each

    When I running a loop: For Each File As System.io.FileI nfo 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.
  • Herfried K. Wagner [MVP]

    #2
    Re: For each

    "cj" <cj@nospam.nosp am> schrieb:[color=blue]
    > When I running a loop: For Each File As System.io.FileI nfo 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.[/color]

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

    Comment

    • cj

      #3
      Re: For each

      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:[color=blue]
      > "cj" <cj@nospam.nosp am> schrieb:[color=green]
      >> When I running a loop: For Each File As System.io.FileI nfo 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.[/color]
      >
      > I am curious why you are not using a 'For...To' loop.
      >[/color]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: For each

        "cj" <cj@nospam.nosp am> schrieb:[color=blue]
        > 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.[/color]

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

        Comment

        • Ken Tucker [MVP]

          #5
          RE: For each

          Cj,

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

          Ken
          -------------------
          "cj" wrote:
          [color=blue]
          > When I running a loop: For Each File As System.io.FileI nfo 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.
          >[/color]

          Comment

          • Yuan Ren[MSFT]

            #6
            Re: For each

            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

            Comment

            • CMM

              #7
              Re: For each

              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.nosp am> wrote in message
              news:OzS%23G00K GHA.3856@TK2MSF TNGP12.phx.gbl. ..[color=blue]
              > 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:[color=green]
              >> "cj" <cj@nospam.nosp am> schrieb:[color=darkred]
              >>> When I running a loop: For Each File As System.io.FileI nfo 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.[/color]
              >>
              >> I am curious why you are not using a 'For...To' loop.
              >>[/color][/color]


              Comment

              • CMM

                #8
                Re: For each

                The Proper way to code this would be to use a a Do Loop and control the
                increment manually.

                Do Until whatever_condit ion

                'do whatever work on
                'fileInfo(i)

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


                "Ken Tucker [MVP]" <KenTuckerMVP@d iscussions.micr osoft.com> wrote in message
                news:82F4B6F3-6999-43FD-BFC2-84CBF093BD17@mi crosoft.com...[color=blue]
                > Cj,
                >
                > A for each loop will only work forwards. You will need to use a
                > for
                > next loop.
                >
                > Ken
                > -------------------
                > "cj" wrote:
                >[color=green]
                >> When I running a loop: For Each File As System.io.FileI nfo 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.
                >>[/color][/color]


                Comment

                • Cor Ligthert [MVP]

                  #9
                  Re: For each

                  cj
                  [color=blue]
                  > When I running a loop: For Each File As System.io.FileI nfo In FileInfo Is
                  > there a way to tell which file number (index) I'm currently on?[/color]

                  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.



                  The For Next Index gives all the information direct.

                  I hope this helps,

                  Cor


                  Comment

                  • Cor Ligthert [MVP]

                    #10
                    Re: For each

                    > The Proper way to code this would be to use a a Do Loop and control the[color=blue]
                    > increment manually.
                    >
                    > Do Until whatever_condit ion
                    >
                    > 'do whatever work on
                    > 'fileInfo(i)
                    >
                    > If whatever Then
                    > i += 1
                    > Else whatever2 then
                    > i -= 3
                    > ...
                    > End If
                    > Loop
                    >[/color]
                    Brrrrrrrrrrrrrr rr, proper for who, an assembler programmer, who learned less
                    or nothing from possibilities from higher languages.

                    Cor


                    Comment

                    • CMM

                      #11
                      Re: For each

                      Did you even read the poster's question?


                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: For each

                        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


                        Comment

                        • Cor Ligthert [MVP]

                          #13
                          Re: For each

                          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


                          Comment

                          • cj

                            #14
                            Re: For each

                            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:[color=blue]
                            > When I running a loop: For Each File As System.io.FileI nfo 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.[/color]

                            Comment

                            Working...