471,350 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

more about arrays

Hello!

I have read in a book the following.
The last line of this method Shuffle shown below uses the CopyTo() method of
the System.Array class(used whenever you create an array) to copy each of
the Cards in newDeck back into cards. This means that you are using the same
set of cards object
in the same cards object rather than creating any new instances. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!

Now to my question is it safe to replace newDeck.CopyTo(cards, 0);
with cards = newDeck in the last row in the method Shuffle below..

The text above is mentioned something about not being safe. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!

So what is the author trying to say with this This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!
public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);
}

//Tony
Jun 27 '08 #1
1 947
The quote means that after execution of the statement "Cards =
newDeck", Cards will be pointing to (referencing) a different object,
and thus, any retained references to the old object elsewhere in the
code will not be updated.

Example:
class a { ... }
a[] c = new a[1]; // <----- c is now pointing to an array of objects
a d = c[0]; // <----- d is now pointing to an element in the object
array
c = new a[1]; // <---- c is now pointing to a different array of
objects
// d is still pointing to someplace inside an instant no longer
pointed to by c

Now, this can spell advantage or disaster depending upon whether you
deem such behavior suitable. If there are other references in your
code that need to point to the new instant then use CopyTo() instead.
If you want the other references to retain pointers to the old instant
then, by all means, use Cards = newDeck, it is faster than CopyTo();

Hope this helps,
jake
On Jun 7, 4:49 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Hello!

I have read in a book the following.
The last line of this method Shuffle shown below uses the CopyTo() method of
the System.Array class(used whenever you create an array) to copy each of
the Cards in newDeck back into cards. This means that you are using the same
set of cards object
in the same cards object rather than creating any new instances. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!

Now to my question is it safe to replace newDeck.CopyTo(cards, 0);
with cards = newDeck in the last row in the method Shuffle below..

The text above is mentioned something about not being safe. If you had
instead used cards = newDeck, then you would be replacing the object
instance referred to by cards with another. This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!

So what is the author trying to say with this This could cause problems if
code elsewhere was retaining a reference to the original cards instance -
which wouldn't be shuffled!

public void Shuffle()
{
Card[] newDeck = new Card[52];
bool[] assigned = new bool[52];
Random rand = new Random();

for (int i = 0; i < 52; i++)
{
int destCard = 0;
bool foundCard = false;

while (foundCard == false)
{
destCard = rand.Next(52);
if (assigned[destCard] == false)
foundCard = true;
}
assigned[destCard] = true;
newDeck[destCard] = cards[i];
}
newDeck.CopyTo(cards, 0);

}

//Tony
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Markus Dehmann | last post: by
5 posts views Thread by JezB | last post: by
1 post views Thread by Rob Griffiths | last post: by
5 posts views Thread by akameswaran | last post: by
6 posts views Thread by Robert Bravery | last post: by
weaknessforcats
29 posts views Thread by weaknessforcats | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.