Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 :)

VB
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
Posted
Comments
Sergey Alexandrovich Kryukov 12-Jun-12 23:41pm    
You have 2 "for each". Which one "does not loop"? Do a simple thing: output collection's Count first, and in the loop, comment out everything and do something read-only -- you will see that it's looping as expected. Then think what do you do to the collection inside the loop...
There is not enough information to answer more exactly.
--SA
_Maxxx_ 13-Jun-12 0:05am    
It's the loop in the first three lines of code that doesn't loop - the function is just there to show what's in the List
I have output the count first - it is three, or two, or whatever.
I have commented everything out except for the loop itself - and it does not loop as expected.
"There is not enough information to answer more exactly" - I wish you would let me know what additional information I can give?
ledtech3 12-Jun-12 23:53pm    
Instead of using list(of Datarow) can you use system.Data.DataRowCollection ?
Not Sure how to Use it , just found it in the object browser.

The problem could be in the line
VB
newRow.ItemArray = row.ItemArray

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.

VB
newRow.ItemArray = DirectCast(row.ItemArray.Clone(), Object())
 
Share this answer
 
v2
Comments
_Maxxx_ 13-Jun-12 0:10am    
While ItemArray is a reference type, in fact assigning it causes the contents of the array to be assigned to the individual columns of the receiving row - it does NOT assign a reference to the dataRow.
You could think of it as having a setter that iterates 'value' and sets row.Item(i) for each element in the value array.
Jut to be sure I made the suggested change - no difference :(
Manas Bhardwaj 21-Jun-12 3:17am    
+5!
VJ Reddy 21-Jun-12 3:41am    
Thank you, Manas :)
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?
 
Share this answer
 
Comments
ledtech3 13-Jun-12 11:28am    
So does that mean you need to use "NewRow" Instead of "ImportRow" to get it to add properly to an existing table Accoring to that page?
_Maxxx_ 13-Jun-12 18:55pm    
That's what I ended up having to do - not quite as simple as that in the 'real' situation, as the first row in the collection wasn't always detached - depending on circumstances - so I had to clone the row then add it (as you can't add a row that is attached to another table)
VJ Reddy 13-Jun-12 11:30am    
Good point. 5!
ledtech3 13-Jun-12 11:44am    
This part of that statement Just Sunk in,"but with default values for the row"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900