2013-08-19 59 views
-1

我有一個For Each循環只迭代1個元素。它從我測試中的第6個元素開始,恰好是comp.includeMe評估爲True的一個元素。在執行外部if語句之後,它將開始第二次迭代,但會退出循環並在comp.includeMe計算結果爲false後立即返回。不存在錯誤或警告,並且我已驗證組件對象中有元素。任何人都可以解釋我做錯了什麼,爲什麼這個語法不起作用?爲每個循環VB.NET退出一次迭代後

Public Class BOM  
    Public Property components as New List(Of Component) 

    Public Function TotalArea(ByVal adjusted As Boolean) As Double 
     Dim total As Double = 0 
     For Each comp As Component In components 
      If comp.includeMe = True Then 
       If adjusted Then 
        total += comp.GetAdjustedSize() * comp.quantity 
       Else 
        total += comp.area * comp.quantity 
       End If 
      End If 
     Next 
     Return total 
    End Function 

    public sub Add(byval comp as Component) 

     components.add(comp) 
    end sub 
End Class 

Public Class Component 
    Public Property quantity as Integer 
    Public Property area as Double 
    Public Property includeMe as Boolean 

    ... 
End Class 

' object construction 
Dim bomlist as New BOM 
bomlist.add(comp) 
+0

在哪裏初始化'components'? –

+0

當BOM類被構造時,一個列表被傳入 –

+0

我已經驗證了調試器組件中實際上有值。沒有丟失 –

回答

1

挖得更深一些後,似乎foreach語句被識別第一if語句且僅當拉值這是真的。我意識到我只有一個組件,其includeMe布爾值設置爲true。在我將其他組件設置爲true之後,我觀察到For Each迭代的次數與包含includeMe = True的組件數完全相同

+0

......圖... –

0

我建議加入一些調試語句來幫助調試:

Public Class BOM  
    Public Property components as New List(Of Component) 

    Public Function TotalArea(ByVal adjusted As Boolean) As Double 
     Dim total As Double = 0 
     Debug.Print(components.Count) 
     For Each comp As Component In components 
      Debug.Print(comp.includeMe) 
      If comp.includeMe = True Then 
       If adjusted Then 
        total += comp.GetAdjustedSize() * comp.quantity 
       Else 
        total += comp.area * comp.quantity 
       End If 
      End If 
     Next 
     Return total 
    End Function 

    public sub Add(byval comp as Component) 

     components.add(comp) 
    end sub 
End Class