2013-01-18 43 views
0

我已經將2個元素添加到vb.net集合中。在調試時,我可以看到.count = 2.對於每個循環迭代集合的第一個空元素

如果監視集合中的元素,我通常會看到零索引中的第一個空元素,然後是我添加的兩個元素。

enter image description here

的問題是,當我遍歷集合與一個For Each Next循環,空的元素進行迭代,最後元素是沒有的。

這是結構的聲明方式

Structure bstCategory 
    Dim categoryCode As String 
    Dim categoryVersion As String 
End Structure 

這是集合的填充方式

With ci.tItem.information 
    .categories = New Collection 
    Dim additionalClassification As bstCategory 
    additionalClassification.categoryCode = "1" 
    additionalClassification.categoryVersion = "A" 
    .categories.Add(additionalClassification) 
    additionalClassification.categoryCode = "2" 
    additionalClassification.categoryVersion = "B" 
    .categories.Add(additionalClassification) 
End With 

這是收集的方式重複

For Each category As bstCategory In ci.tItem.information.categories 
     ValidateCategory(categorycode) 
Next 

我在做什麼錯誤?

+0

'昏暗名稱作爲新Microsoft.VisualBasic.Collection() names.Add( 「約翰」) names.Add(2) 對於每個X在名稱 Console.WriteLine(x)的 Next'適用於我。你有沒有嘗試重新啓動VS? –

+0

你可以發佈你的'bstCategory'的代碼嗎?如果它創建了一個類,請檢查代碼 – SysDragon

+0

我已重新啓動VS,但問題仍然存在。 – adrianzz

回答

0

我無法重現您的問題,它工作正常,我用vb.net在VS2010目標.NET 2.0

這裏是我的嘗試根據您的代碼:

Module Module1 

    Structure bstCategory 
     Dim Code As String 
     Dim Version As String 
    End Structure 

    Sub Main() 

     Dim categories As New Collection() 
     Dim c As New bstCategory 
     c.Code = "1" 
     c.Version = "1" 
     categories.Add(c) 
     categories.Add(c) 

     For Each category As bstCategory In categories 
      Console.WriteLine(category.Code) 
     Next 

    End Sub 

End Module 

如果每個人都在給你那種奇怪的行爲,你可以改變它來使用For。

For i As Integer = 1 To categories.Count 
    Console.WriteLine(categories(i).Code) 
Next 

無論如何,這是非常奇怪的你面對什麼,我的建議是尋找在谷歌索引0文本。我做到了,有一些網頁在討論,所以你的答案可能在那裏。

谷歌爲:「空的佔位符調整爲1基於陣列」

希望幫助!

這裏是一張圖片,顯示了從0到2的集合中的3個值,但是,當迭代時,它只輸入兩次,打印輸出兩次。

enter image description here

+0

如果我使用for而不是For Each它的工作,但我已經使用了很多對於通過相同代碼的每個循環,並且如果一直困擾我長時間工作錯誤 – adrianzz

相關問題