Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preserving Columns in DataRow Passed toa Subroutine

Status
Not open for further replies.

Auguy

Programmer
May 1, 2004
1,206
US
I know if I pass a row to a subroutine using
Code:
ByVal Row2 as DataRow
I can change the columns in Row2 and it will change the original row column. How do I change the columns of Row2 in the subroutine without changing the values in the original row of the calling program? Do I have to declare another datarow and set that datarow = Row2 in the subroutine?

Auguy
Sylvania/Toledo Ohio
 
Actually, you have it backwards. Passing ByVal (by value) passes a copy of the DataRow, so any changes made to it are lost after the procedure exits. Passing ByRef (by reference) passes the actual DataRow, and any changes made will persist after the procedure exits.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
I used to think that, but for reference type parameters using ByVal that is not true. check this out I found this out when checking one of my subroutines and could not figure out why it DID change the "sentTo" column value. The value of the column "SentTo" is "TRACK" after returning to the calling program.
Code:
' Calling Program
Pro404Row("SentTo") = ""
KS_TrackOnly(ModeType, Pro404Row, dsProFileRouting, TrackStrBldr, NowDateTime)
' The value of the SentTo column is "TRACK" after returning from the subroutine
Code:
Private Sub KS_TrackOnly(ByVal ModeType As String, _
    ByVal Pro404Row2 As DataRow, _
    ByVal dsProFileRouting2 As DataSet, _
    ByVal StrBldr As System.Text.StringBuilder, _
    ByVal FileDateTime As DateTime)
...
Pro404Row2("SentTo") = "TRACK"
End Sub
I just tested it a bunch of times. Maybe there's something I'm missing. I always assumed that ByVal meant you could not change the original passed values.


Auguy
Sylvania/Toledo Ohio
 
The easiest way to show the difference between ByRef and ByVal:
One button on the Form

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) _
                          Handles Button1.Click
        Dim intX As Integer = 5
        Call AddThis(intX)
        MessageBox.Show(intX)
    End Sub

    Private Sub AddThis([blue]ByRef[/blue] MyValue As Integer)
        MyValue = MyValue + MyValue
    End Sub
End Class

Then change [tt][blue]ByRef[/blue][/tt] to [tt][blue]ByVal[/blue][/tt] and see the difference.

Have fun.

---- Andy
 
Well, look at that, I learned something new today! Thanks for that link.

Now, to address your problem, you could call RejectChanges on the DataRow when you are done with it in the sub.

Private Sub Foo(ByVal dr As DataRow)
'do some stuff that changes the values in the columns of the DatRow

dr.RejectChanges 'undo all changes made since the last time AcceptChanges was called
End Sub



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Me too!. I only found it after reviewing some old code I wanted to change and couldn't figure out why it WAS working. Kind of makes me want to go review a lot of my code to see where this might jump up and bite me. Most of the time I'm just passing "Value" parameters like integers and small text. Thanks, Reject Changes might work but I will have to make sure I'm not throwing away some changes I do want.

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top