xShareMaster
xShareMaster

Reputation: 13

Copy Array Elements

I have strongly typed class,

Public Class RowData

    Sub New(ByVal rRecord As String, ByVal rAccount As String, _
            ByVal soExperian As Boolean, ByVal soEquifax As Boolean, ByVal soTransunion As Boolean, _
            ByVal snExperian As Boolean, ByVal snEquifax As Boolean, ByVal snTransunion As Boolean)

        Me.Record = rRecord
        Me.Account = rAccount
        Me.oExperian = soExperian
        Me.oEquifax = soEquifax
        Me.oTransunion = soTransunion
        Me.nExperian = snExperian
        Me.nEquifax = snEquifax
        Me.nTransunion = snTransunion

    End Sub

.........

End Class

Then I use the following code to declare 2 elements array.

Dim Tradelines(1) As List(Of RowData)

Tradelines(0) = New List (Of RowData)
Tradelines(0).Add(New RowData("222", "222", False, False, False, False, False, False))

Tradelines(0) is populated, no problems, but when I use the following code to copy Element-0 to Element-1 they seem to be bound so that if I change any value in either element, the other element updates automatically. I do not want that, any clue?

Tradelines(1) = Tradelines(0)

Upvotes: 1

Views: 181

Answers (2)

mohammad
mohammad

Reputation: 1

try this

Tradelines(0).ForEach(AddressOf Tradelines(1).Add)

Upvotes: 0

Tomislav Markovski
Tomislav Markovski

Reputation: 12346

The items in your collections of type RowData are passed by reference, they are not value types. That's why changing one, will make changes in all collections the item is added, because it's the same object.

You need to create new objects and add them to the new collection if you don't want the same references. One way to this in a good way is to implement IClonable in your RowData class.

Tradelines(1) = Tradelines(0).Clone()

Upvotes: 1

Related Questions