2012-02-29 123 views
1

所以問題是,我有2個循環嵌套在另一個。外循環調用一個過程,內循環爲過程設置一個屬性供使用。問題是程序是我不想退出循環,使用pos - < - 屬性調用過程並重新進入內部循環。由於所有條件在內部循環內都是真實的,所以每次只更換一次pos,所以pos僅在一次設置。我希望能夠設置內部循環的pos出口,調用過程並重新進入內部循環並將其設置爲不同的值?任何幫助將是偉大的!這裏是代碼內部循環,程序調用混淆

For Each val As String In vals 


     If creditPoints = "20" And semester = "1" And year = "Year 1" Then 

      For Each position In MyPosList 

       If position.strLabel = "a1" And available(0) <> "False" Then 
        pos = position.strX & " " & position.strY 
        count += 1 
        available(0) = blnavailable 
       ElseIf position.strLabel = "b1" And available(1) <> "False" Then 
        pos = position.strX & " " & position.strY 
        count += 1 
        available(1) = blnavailable    
Next 
shortfat(semester, pos, creditPoints, title, year, modStatus, count) 
     End If 
next 
+0

太多的代碼,請寫一個簡短的例子。 – vulkanino 2012-02-29 13:12:42

+0

多數民衆贊成它縮短 – aspiringCoder 2012-02-29 13:18:44

+0

爲什麼不調用內循環的過程? – Henrik 2012-02-29 13:22:56

回答

0

當你滿足這些條件之一時,你只是想打破內循環?如果是這樣,那麼這就是Exit關鍵字的用途。你也應該設置一個標誌來保持健康。

''//Flag so that we know if we actually found a position 
Dim FoundPosition as Boolean 
For Each val As String In vals 
    ''//Reset the flag and assume that are conditions are met 
    FoundPosition = False 
    If creditPoints = "20" And semester = "1" And year = "Year 1" Then 
     For Each position In MyPosList 
      If position.strLabel = "a1" And available(0) <> "False" Then 
       pos = position.strX & " " & position.strY 
       count += 1 
       available(0) = blnavailable 
       ''//Flag that our conditions are met 
       FoundPosition = True 
       ''//Exit from the inner loop 
       Exit For 
      ElseIf position.strLabel = "b1" And available(1) <> "False" Then 
       pos = position.strX & " " & position.strY 
       count += 1 
       available(1) = blnavailable 
       ''//Flag that our conditions are met 
       FoundPosition = True 
       ''//Exit from the inner loop 
       Exit For 
      End If 
     Next 
     ''//Sanity check to ensure that our conditions are met 
     If FoundPosition Then 
      shortfat(semester, pos, creditPoints, title, year, modStatus, count) 
     Else 
      ''//Do something here, either Throw an error or safely handle this case otherwise 
     End If 
    End If 
Next 
+0

有沒有什麼辦法可以使用布爾值退出循環?所以如果如果如果找到的位置設置爲真,然後停止循環? – aspiringCoder 2012-03-06 21:15:23

+0

@Stefan Reaney,我不知道你的意思。 「退出」是你如何早早擺脫循環。你可以在你想要的條件下做到這一點,所以如果你想檢查一個布爾值,那麼我猜吧。 – 2012-03-07 04:37:47

0

「我希望能夠設置POS退出內部循環,調用 程序並重新進入內循環,並將其設置POS爲不同的 價值?」

這樣的事情比WHILE循環更適合於FOR循環。

只是在這裏黑暗中刺,但它聽起來像你想要能夠看到如果shortfat正在產生有利的價值。如果沒有,你想要它重新計算pos。我會做的第一件事是,改變shortfat返回某種價值......對於我的例子,我會讓它返回一個布爾值。

Dim blnDidThisDoWhatIWant As Boolean 

    For Each val As String In vals 
     If creditPoints = "20" And semester = "1" And Year() = "Year 1" Then 
      blnDidThisDoWhatIWant = False 

      While blnDidThisDoWhatIWant = False 
       For Each position In MyPosList 
        If position.strLabel = "a1" And available(0) <> "False" Then 
         pos = position.strX & " " & position.strY 
         count += 1 
         available(0) = blnavailable 
        ElseIf position.strLabel = "b1" And available(1) <> "False" Then 
         pos = position.strX & " " & position.strY 
         count += 1 
         available(1) = blnavailable 
        End If 
       Next 

       blnDidThisDoWhatIWant = shortfat(semester, pos, creditPoints, title, Year, modStatus, count) 
      End While 
     End If 
    Next 

這將允許您重新進入內部循環。問題是,它將是無限的,除非位置是不同的計算(我不明白它是如何)。所以這是你必須自己解決的問題。希望這有助於您指出正確的方向。