更新类的一个实例会导致另一个实例更新

时间:2018-02-02 13:42:51

标签: c# visual-studio

我正在编写一个基本程序,模拟一副牌并将牌拉入手中。在绘制之后,我给卡片组中的类变量,为卡片绘制0和null的值,为rank和suit,作为检查卡片是否从卡片中抽出。但是,当我将卡片组中的卡片设置为0并且为空时,它也会将手形阵列中的卡片设置为相同。任何解决问题的建议都非常感谢。谢谢!

Draw Card按钮的代码:

var array = ["up", "down", "up", "pause", "up", "pause", "down", "created"];

// Define the flags
const flags = {
  up:      1 << 0,
  down:    1 << 1,
  pause:   1 << 2,
  created: 1 << 3,
};

// Set the flags
var f = array.reduce((f, s) => f | flags[s], 0);

// Interpret the flags
var pause = f == flags.up;
var stop  = hasOnly(f, flags.pause | flags.up);
var start = f == flags.created || hasOnly(f, flags.pause | flags.down);
var kill  = pause || stop || start;

console.log(start + " // " + pause + " // " + stop + " // " + kill);

function hasOnly(f, mask) {
   return (f & ~mask) == 0 && (f & mask) != 0
}

addToHand函数:

Card drawn = new Card(0, null);
        //
        do
        {
            i++;
            drawn = test.cards[i];
        } while (drawn.rank == 0 || drawn.suit == null);
        //
        output = drawn.rank + " " + drawn.suit;
        lblOutput.Text = output;
        //
        addToHand(drawn);
        if (handCount < 5)
        {
            handCount++;
        }
        test.cards[i].suit = null;
        test.cards[i].rank = 0;
        //
        if (i >= 51)
        {
            MessageBox.Show("You have drawn all the cards.");
            btnDraw.Enabled = false;
            btnShuffle.Enabled = false;
        }

以下是套牌和牌的类别:

private void addToHand(Card drawn)
    {
        if (handCount < 5)
        {
                if (hand[0].suit == null)
                {
                    hand[0] = drawn;
                    button1.Text = hand[0].rank + " " + hand[0].suit;
                    button1.Enabled = true;
                }
                else if (hand[1].suit == null)
                {
                    hand[1] = drawn;
                    button2.Text = hand[1].rank + " " + hand[1].suit;
                    button2.Enabled = true;
                }
                else if (hand[2].suit == null)
                {
                    hand[2] = drawn;
                    button3.Text = hand[2].rank + " " + hand[2].suit;
                    button3.Enabled = true;
                }
                else if (hand[3].suit == null)
                {
                    hand[3] = drawn;
                    button4.Text = hand[3].rank + " " + hand[3].suit;
                    button4.Enabled = true;
                }
                else if (hand[4].suit == null)
                {
                    hand[4] = drawn;
                    button5.Text = hand[4].rank + " " + hand[4].suit;
                    button5.Enabled = true;
                }
        }

2 个答案:

答案 0 :(得分:1)

C#对象本身是通过引用传递的,而不是通过值传递。所以当你这样做时:

Card card = cards[0];

您没有创建新卡,而是引用已创建的卡。因此,当您更改cards [0]的值时,对该值的每次引用都会更改。

您可能想要“克隆”它,或者在您的情况下,只需复制卡中的所有数据。

Card card = new Card();
card.suit = cards[0].suit;
card.rank = cards[0].rank;

任何不是值类型的东西都需要以相同的方式克隆。

答案 1 :(得分:0)

当你写hand[0] = drawn;手[0]并且绘制的是同一个对象时。 如果你想拥有相同的值但是在两个不同的对象中你可以:

  • 使用复制构造函数:new Hand(drawn)
  • 使用绘制的对象的Clone方法(要求ICloneable)。

您可能会对IClonneable界面感兴趣:

https://msdn.microsoft.com/en-us/library/system.icloneable.clone(v=vs.110).aspx

还有复制构造函数:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor