eMbs
eMbs

Reputation: 79

VB.NET array = array

I'm trying to create a program in which I'm assigning array to array. They are declared like that:

Const Deck_Size = 52

Private Deck(Deck_Size), Table_Deck(Deck_Size) As String

So, in my program I have written this line:

Deck = Table_Deck

And what this does, how i can understand from results I'm getting, that "Table_Deck" pointer have been assign to "Deck". For e.g.

Table_Deck = "As","Ks","Qs","Js",...

Deck = "2h", "3h", "4h", "5h",...

After this code line: Deck = Table_Deck, I have this:

Table_Deck = "As","Ks","Qs","Js",...

Deck = "As","Ks","Qs","Js",...

And here comes the problem.

When the next code line is done (which is: Table_Deck(1) = Table_Deck(4)), I get this result: Table_Deck = "Js","Ks","Qs","Js",..., but as well "Deck" changes too...

Deck = "Js","Ks","Qs","Js",...

So, I assume that this is pointers fault. Can anyone tell me how I can solve this problem, if I want to change element in only one array.

Upvotes: 1

Views: 233

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

Right, your assignment changes the array reference. Afterwards, both Deck and Table_Deck reference the exact same array. So any changes you make to Deck's content will be visible through the Table_Deck reference as well. You have to copy the content instead:

    Table_Deck.CopyTo(Deck, 0)

Upvotes: 4

Related Questions