views:

472

answers:

1

I am trying to reference a DataTable and I do not know how to achieve this. Please help and thank you very much.

In my code, I would like to assign dtTmp to dtTarget(i) through the reference dtCurr. Is it possible to achieve that in VB.NET?

Private dtA, dtB As DataTable

Dim dtTarget() As DataTable = {Me.dtA, Me.dtB}

For i As Integer = 0 To dtTarget.Length - 1

  dtTmp = New DataTable()
  'Do something to dtTmp

  'assign the new table to the reference
  dtTarget(i) = dtTmp      

Next
+1  A: 

dtCurr = dtTmp changes the reference of dtCurr, which is pointer you don't use later. Try writing dtTarget(i) = dtTmp instead of your last line.

Kobi
Thanks for your answer. I should clarify my problem.I have two private attributes:Private dtA, dtB As DataTableThen I start an array:Dim dtTarget(1) = {Me.dtA, Me.dtB}After running the code, the original Me.dtA and Me.dtB does not change. Using dtTarget(i) = dtTmp gives the same result.
Timothy Chung
I have edited my question, so you now know my actual problem. Thanks. :-)
Timothy Chung
Truth. That's not how references work. dtA still holds a reference to the original DataTable. You could try `dtA = dtTarget(1)`, but that's very roundabout.
Kobi