I think I'm missing something fundamental about how DataTables work. The following procedure starts with GetData, bringing in what as a filled DataTable. All the following code does is pass through copies of the datatable, manipulate it, then return it:
Sub GetData(ByVal what As DataTable)
Dim Part As DataTable = Generate(what)
End Sub
Function Generate(ByVal brown As DataTable)
Dim lameface As DataTable = DoStuff(brown)
Return lameface
End Function
Function DoStuff(ByVal cow As DataTable)
Dim result As DataTable = cow
result.Rows.RemoveAt(0)
Return result
End Function
The way this is written above, function DoStuff will remove the top row from result and cow. Similarly, brown and what will also have that first row removed, even though they are sent as ByVal.
If I change the first line in DoStuff from
Dim result As DataTable = cow
to
Dim result As DataTable = cow.copy
then cow, brown and what are left alone. Why is this? Marking a parameter as ByVal is supposed to send a copy of the object instead of the original, so why do I have tell it to use a copy when instantiating result? If I do a similar procedure using integers instead of datatables, it works as I would expect. What am I missing about datatables?
I poked around on MSDN's articles for datatables and didn't see anything that spoke to this. Thanks for your help!