# Iterating Over Dynamic Number of Nested Loops

Page 1 of 1

## 1 Replies - 118 Views - Last Post: 28 February 2018 - 06:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=409574&amp;s=b6a9ee67abecf6c93312e25a7624e0b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 mtnerd594

Reputation: 0
• Posts: 20
• Joined: 22-May 16

# Iterating Over Dynamic Number of Nested Loops

Posted 28 February 2018 - 05:24 PM

I am trying to learn about Dynamic Number of Nested Loops.

I read the article that is posted here

Then I converted it to VB.Net. The issue is, it only does the red-apple-one & red-apple-two. I am not sure why it does not proceed further.

Here is my code:

```Public Class frmTest

End Sub

Public Shared Iterator Function IterateDynamicLoop(Of t)(ByVal data As IList(Of List(Of t))) As IEnumerable(Of IEnumerable(Of t))

Dim count As Integer = data.Count
Dim loopIndex = count - 1
Dim counters = New Integer(count - 1) {}
Dim bounds = data.[Select](Function(x) x.Count).ToArray()

Do
Yield Enumerable.Range(0, count).[Select](Function(x) data(x)(counters(x)))

Loop While IncrementLoopState(counters, bounds, loopIndex)

End Function

Private Shared Function IncrementLoopState(ByVal counters As IList(Of Integer), ByVal bounds As IList(Of Integer), ByRef loopIndex As Integer) As Boolean

If loopIndex < 0 Then Return False

counters(loopIndex) = counters(loopIndex) + 1
Dim result = True

If counters(loopIndex) >= bounds(loopIndex) Then

counters(loopIndex) = 0
loopIndex = -1
result = IncrementLoopState(counters, bounds, loopIndex)
loopIndex += 1

End If

Return result

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim data As New List(Of List(Of String)) From {New List(Of String) From {"red", "green", "blue"},
New List(Of String) From {"apple", "banana", "peach", "melon"},
New List(Of String) From {"one", "two"}}

For Each item In IterateDynamicLoop(Data).[Select](Function(x) String.Join("-", x))

Next

End Sub
End Class
```

Thank you

Is This A Good Question/Topic? 0

## Replies To: Iterating Over Dynamic Number of Nested Loops

### #2 IronRazer

• Custom Control Freak

Reputation: 1475
• Posts: 3,734
• Joined: 01-February 13

## Re: Iterating Over Dynamic Number of Nested Loops

Posted 28 February 2018 - 06:52 PM

First I would recommend turning Option Strict on so that you can see all the Type conversion errors there are in that code and fix them. You should also be aware that code converters are not always 100% accurate at converting code so, it helps to understand C# and VB.Net so you can fix any code that the converter fails to convert correctly. Probably the most important thing to learn is how to debug your code so you can find where it is doing something unexpected or giving an unexpected result.

Here is a link to a tutorial to learn how to debug your code that you can check out.
Debugging Express

Compare this to what you have so you can see the changes I have made to the code. That will help see where some of the code could be going wrong.
```Option Strict On
Option Explicit On
Option Infer Off

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim data As New List(Of List(Of String)) From {New List(Of String) From {"red", "green", "blue"}, New List(Of String) From {"apple", "banana", "peach", "mellon"}, New List(Of String) From {"one", "two"}}
For Each item As String In IterateDynamicLoop(data).[Select](Function(x) String.Join("-", x))
RichTextBox1.AppendText(item & Environment.NewLine)
Next
End Sub

Public Iterator Function IterateDynamicLoop(Of T)(ByVal data As IList(Of List(Of T))) As IEnumerable(Of IEnumerable(Of T))
Dim count As Integer = data.Count
Dim loopIndex As Integer = count - 1
Dim counters(count - 1) As Integer
Dim bounds() As Integer = data.Select(Function(x) x.Count).ToArray
Do
Yield Enumerable.Range(0, count).Select(Function(x) data(x)(counters(x)))
Loop While IncrementLoopState(counters, bounds, loopIndex)
End Function

Private Function IncrementLoopState(ByVal counters As IList(Of Integer), ByVal bounds As IList(Of Integer), ByRef loopIndex As Integer) As Boolean
If loopIndex < 0 Then Return False
counters(loopIndex) = counters(loopIndex) + 1
Dim result As Boolean = True
If counters(loopIndex) >= bounds(loopIndex) Then
counters(loopIndex) = 0
loopIndex -= 1
result = IncrementLoopState(counters, bounds, loopIndex)
loopIndex += 1
End If
Return result
End Function
End Class

```