title: Recursive which ends itself
body: <p>I want to search within a room for rooms (I dont know how many rooms are in a room).</p>
  
  <p>Then for each room in this room I want to search again if there are rooms in these rooms and so on (recursive).</p>
  
  <p>But if a room depth level reaches X, it should stop/save OR if there are no more rooms. My code basically works but it overrides a room when there are more rooms in it.</p>
  
  <p>So let's say a room A has two rooms Room 1 and Room 2. Then my function will work with Room 1 but ignores Room 2 and I don't know why.</p>
  
  <pre><code>    Private Class Room
      Public Name As String
      Public RoomCounter As Integer
  End Class
  
  Private Const _maxRooms As Integer = 2
  
  Private Sub GetRoomsRecursive(pMatchString As String, pCurrentRound As Integer, pOriginalRoom As Room)
  
      Dim foundRooms As String = RoomsMatching(pMatchString)
  
      For Each s As String In foundRooms.Split(vbNewLine)
          If Not s.Contains(";") Then 'There are rooms in that room
              If pCurrentRound &gt;= _maxRooms - 1 Then Continue For 'Don't search further
  
              If pOriginalRoom Is Nothing Then
                  Dim tempRoom As New Room With {.Name = $"Room_{s}"}
                  tempRoom.RoomCounter += 1
                  GetRoomsRecursive(pMatchString, pCurrentRound + 1, tempRoom)
              Else
                  If pOriginalRoom.RoomCounter = _maxRooms Then Continue For '
                  pOriginalRoom.Name = $"Room_{s}" 'Override name
                  pOriginalRoom.RoomCounter += 1
                  GetRoomsRecursive(pMatchString, pCurrentRound + 1, pOriginalRoom)
                  End If
              Else 'There are no more rooms
                   If pOriginalRoom Is Nothing Then
                      Dim tempRoom As New Room With {.Name = $"Room_{s}"}
                      tempRoom.RoomCounter += 1
                     'todo: save the room
                  Else
                      If pOriginalRoom.RoomCounter = _maxRooms Then Continue For '
                      pOriginalRoom.Name = $"Room_{s}" 'Override name
                      pOriginalRoom.RoomCounter += 1
                      'todo: save the room
                  End If
              End If
          Next
      End Sub
  </code></pre>