Pointing Problems
private void button1_Click(object sender, EventArgs e)
{
Card Card1 = new Card();
Card Card2 = new Card();
Card1.Value = 3;
Card1.Suit = 3;
Card2 = Card1;
Card2.Suit = 2;
}
public class Card
{
public int Suit;
public int Value;
public Card()
{
Suit = 0;
Value = 0;
}
}
I step through the Button1 code and any changes on Card2 are updated across to Card1 like they were the same or if card2 were simply pointing to card1. What have I done wrong?
THanks
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Roei BarRank #1,271 · 1.4K reputationPosted Aug 31, 2009, 12:15 PM EDT
Card2 = Card1
when you did that you actually told the compiler to put a pointer from Card1 to Card2
meaning that Card2 is actually Card1.
Kay4 reputationPosted Aug 31, 2009, 12:17 PM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 1, 2009, 6:22 AM EDT
private void button1_Click(object sender, EventArgs e) { Card Card1 = buildBaseCard(); Card Card2 = buildBaseCard(); Card2.Suit = 2; } private Card buildBaseCard() { Card baseCard = new Card(); baseCard.Value = 3; baseCard.Suit = 3; return baseCard; } public class Card { public int Suit; public int Value; public Card() { Suit = 0; Value = 0; } }