简体   繁体   中英

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 eg

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM