Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Variable number of loop nesting

Variable number of loop nesting

Scheduled Pinned Locked Moved Visual Basic
question
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cstrader232
    wrote on last edited by
    #1

    Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks

    L D 2 Replies Last reply
    0
    • C cstrader232

      Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      depending on circumstances recursion, reflection or a simple foreach may do it. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      • C cstrader232

        Strange question, but I'm sure someone's dealt with it. When there are say 2 dimensions, and we want to loop within each of the 2, we write for a = 0 to x for b = 0 to x do something... next next And if there are 3 dimensions, we just add a c loop within the b loop, etc. What if we don't know ahead of time how many dimensions there are? Can we write the looping structure to be dynamic based on a specified number of required loops? Thanks

        D Offline
        D Offline
        dan sh
        wrote on last edited by
        #3

        That would be awful. Unless you just cannot avoid it, don't use it. What if there are 10 nested loops each going from 0 to 1000? If there will be no code between the two loops, you can do something like this:

        for(int i = 0; i
        Then in the Looper method:

        private void Looper(loopFrom,loopTill){
        for(int i=loopFrom;i

        Looks awful.

        You can also generate dynamic code blocks and inject when required.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        C 1 Reply Last reply
        0
        • D dan sh

          That would be awful. Unless you just cannot avoid it, don't use it. What if there are 10 nested loops each going from 0 to 1000? If there will be no code between the two loops, you can do something like this:

          for(int i = 0; i
          Then in the Looper method:

          private void Looper(loopFrom,loopTill){
          for(int i=loopFrom;i

          Looks awful.

          You can also generate dynamic code blocks and inject when required.

          50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

          C Offline
          C Offline
          cstrader232
          wrote on last edited by
          #4

          Thanks for the ideas. I figured out a way to do this with a collection. Roughly, you make your own looping code: Make a collection with the same number of rows as you have dimensions Each row has a property indicating its number of iterations Make a method getvalue in the collection When you call getvalue, if it's the last row in the collection (innermost loop which is incremented on every call) then increment the index by 1 if we're at the highest value of the dimension, then if we're not in the first row of the collection then return to zero increment the value of the index in the prior row (return to zero if necessary) Return the value Works like a charm

          D 1 Reply Last reply
          0
          • C cstrader232

            Thanks for the ideas. I figured out a way to do this with a collection. Roughly, you make your own looping code: Make a collection with the same number of rows as you have dimensions Each row has a property indicating its number of iterations Make a method getvalue in the collection When you call getvalue, if it's the last row in the collection (innermost loop which is incremented on every call) then increment the index by 1 if we're at the highest value of the dimension, then if we're not in the first row of the collection then return to zero increment the value of the index in the prior row (return to zero if necessary) Return the value Works like a charm

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            I don't know which .Net version are you using. If that is 3.0 or above, use LINQ it will surely help you get rid of some loops.

            50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups