VB.Net对于每个循环不循环= Eek! [英] VB.Net For Each loop not looping = Eek!
问题描述
对于每个循环,我都有一个VB.Net,它在包含两个或多个元素的List(DataRow)上进行迭代,并且不对集合进行迭代-好像在该列表中只有一个元素一样(DataRow的)列表
在下面的代码段中,当我在调试中运行时,停止在每一行:
我可以看到行具有多个元素(至少两个元素-我已经尝试了三个或更多元素)
如果再按F10键进入步骤,则转至importRows行-到目前为止一切顺利.
但是,再次按F10会跳到此处的...更多代码-这样就不会循环
没关系someTable.ImportRow(myRow)行上的代码是什么-所以不是那会引起问题-实际上,如果我注释掉该行,它仍然不会循环. >
我觉得我缺少明显的东西-但是(显然)找不到它!
是的,我已经重新启动了:)
I have a VB.Net For Each loop, iterating over a List (Of DataRow) that contains two or more elements, and it doesn''t iterate over the collection - acting as if there were only a single element in the List (of DataRow)
In the code snippet below, when I run in debug, and stop at the for each line:
I can see that rows has more than one element (t least two - I have tried with three and more)
If I then press F10 to step, it goes to the importRows line - so far so good.
Pressing F10 again, though, skips down to the ... more code here - so it doesn''t loop
it doesn''t matter what code is on the someTable.ImportRow(myRow) line - so it''s not that that is causing an issue - indeed if I comment the line out it still doesn''t loop.
I feel I am missing something obvious - but (evidently) can''t find it!
And yes, I have rebooted :)
Dim rows As List(Of DataRow) = myEntryControl.GetRowCollectionFromEntryControl(_Row)
For Each myRow As DataRow In rows
someTable.ImportRow(myRow)
Next myRow
... more code here
Public Function GetRowCollectionFromEntryControl(ByVal row As DataRow) As List(Of DataRow)
Dim rows As New List(Of DataRow)
Dim myGuid As Guid = Guid.NewGuid
rows.Add(row)
row("RelationshipGuid") = myGuid
If _entryControl.AdditionalServices IsNot Nothing Then
For Each serviceType As ClientServiceType In _entryControl.AdditionalServices
' clone the row
Dim newRow As DataRow = row.Table.NewRow()
newRow.ItemArray = row.ItemArray
newRow("ServiceTypeId") = serviceType.Id
newRow("ProgramId") = serviceType.Programs(0).Id
rows.Add(newRow)
Next
End If
Return rows
End Function
推荐答案
问题可能出在线下
The problem could be in the line
newRow.ItemArray = row.ItemArray
ItemArray
是引用类型,如上所述分配时,会将row.ItemArray
保留的引用分配给newRow.ItemArray
,该引用实际上是指elements of the row.
请尝试以下代码.
The ItemArray
is a reference type and assigning as above assigns the reference held by row.ItemArray
to newRow.ItemArray
, which essentially refers to the elements of the row.
Please try the following code.
newRow.ItemArray = DirectCast(row.ItemArray.Clone(), Object())
引人入胜.
我在循环中添加了ShowMessage("Hello There")
它循环了三遍.
我注释掉了ShowMessage
它根本没有循环
我认为这一定是怎么回事:
来自ImportRow上的docco
"
Fascinating.
I added in the loop a ShowMessage("Hello There")
And it looped three times.
I commented out the ShowMessage
It didn''t loop at all
I think what must be happening is this:
From the docco on ImportRow
"
If the DataRow that is passed as a parameter is in a detached state, it is ignored, and no exception is thrown.
"
经检查,除第一行外,所有其他状态均为分离状态.
因此,以某种方式,在运行时,它甚至不需要迭代集合的其余部分,因为它不需要这样做.
如果确实如此,这绝对令人着迷-在这种情况下,运行时真的可以选择不执行循环吗?
当然不是吗?
"
On inspection, all but the first row are in a detached state.
So, somehow, at runtime, it doesn''t even iterate the rest of the collection because it doesn''t need to.
This is absolutely fascinating if this is really the case -can the runtime really choose not to execute a loop in these circumstances?
Surely not?
这篇关于VB.Net对于每个循环不循环= Eek!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!