Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新类的一个实例会导致另一个实例更新_C#_Visual Studio - Fatal编程技术网

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

C# 更新类的一个实例会导致另一个实例更新,c#,visual-studio,C#,Visual Studio,我正在写一个基本的程序,模拟洗牌一副牌和把牌拉到手上。在绘制之后,我在卡片组数组中为绘制的卡片提供0和null的值,以检查卡片是否从卡片组中绘制。但是,当我将牌组数组中的牌设置为0和null时,它也会将手牌数组中的牌设置为相同。我们非常感谢您对解决这个问题的任何建议。谢谢 “提款卡”按钮的代码: Card drawn = new Card(0, null); // do { i++; drawn =

我正在写一个基本的程序,模拟洗牌一副牌和把牌拉到手上。在绘制之后,我在卡片组数组中为绘制的卡片提供0和null的值,以检查卡片是否从卡片组中绘制。但是,当我将牌组数组中的牌设置为0和null时,它也会将手牌数组中的牌设置为相同。我们非常感谢您对解决这个问题的任何建议。谢谢

“提款卡”按钮的代码:

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;
        }
addToHand功能:

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;
                }
        }
下面是牌组和牌组的等级:

public class Deck
{
    public Card[] cards;

    public Deck() //Fills new array with 52 cards
    {
        cards = new Card[52];
        var index = 0;

        foreach (var suit in new[] { "Spades", "Hearts", "Clubs", "Diamonds", })
        {
            for (var rank = 1; rank <= 13; rank++)
            {
                cards[index++] = new Card(rank, suit);
            }
        }
    }
}

public class Card
{
    public int rank { get; set; }
    public string suit { get; set; }

    public Card(int rk, string st)
    {
        rank = rk;
        suit = st;
    }
}

C对象本质上是按引用传递的,而不是按值传递的。所以当你这样做的时候:

Card card = cards[0];
您不是在创建新卡,而是在引用已创建的卡。因此,当您更改卡[0]的值时,对该值的每个引用都会更改

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

Card card = new Card();
card.suit = cards[0].suit;
card.rank = cards[0].rank;
任何非值类型的内容都需要以相同的方式进行克隆。

当您编写hand[0]=Drawed时;手[0]和绘制的是同一对象。 如果希望在两个不同的对象中具有相同的值,则可以:

使用复制构造函数:新手绘 使用绘制的objet弹射ICloneable的克隆方法。 您可以通过iCloneable界面进行查询:

以及复制构造函数:


这是引用类型的基础。不要将test.cards[i].suit指定为null,而是将整个卡片test.cards[i]设置为null?基本类型(如int和string)是按值传递的,这是错误的。不是基元,只有值类型的基元。您还可以创建任何与Int行为完全相同的结构。感谢您的解释!