2012-10-21 50 views
1

我有这个简单的VB.NET功能:什么是逻辑上对这种错误的VB.NET代码

Dim endval = Convert.ToInt16(googleXMLdocument...<s:currentItemCount>.Value) - 1 
For counter = 0 To endval 
    Dim seller = googleXMLdocument...<s:name>(counter).Value 
    Dim containsValue = ToBeIgnored.AsEnumerable().Any(Function(r) r.Field(Of String)("Ignore") = seller) 
    If containsValue Then 
    Continue For 
    End If 
    row = GoogleResults.NewRow() 
    GoogleResults.Rows.Add(row) 
    GoogleResults.Rows(counter)("Seller") = seller 'sometimes this line throws an exception there is no row at position x 
Next 

在最后一行有时我得到一个异常there is no row at position x。什么会造成这种情况?

回答

2

您的计数器变量看起来不像GoogleResults表的行数。

我猜你正在寻找的东西是这样的:

GoogleResults.Rows(GoogleResults.Rows.Count - 1)("Seller") = seller 

或更直接:

row("Seller") = seller 
1

最后两个For环行要写成这样:

row("Seller") = seller; 
GoogleResults.Rows.Add(row) 

添加行后更改行可能会导致不必要的事件被触发。