C# 将值赋给数组时,所有值都相同

C# 将值赋给数组时,所有值都相同,c#,visual-studio-2015,C#,Visual Studio 2015,请记住,这个问题可能是初学者错误的结果 在我的程序中有4个类与这个问题相关 主要形式:直接在类中声明并启动对象currentRecipe(使用类recipe)和recipeMngr(使用类recipeManager)。 currentRecipe(Recipe)有两个从用户输入中收集的字段,这些字段包括数组成分[],该数组成分[]是从按下按钮时打开的另一种形式的属性中收集的。currentRecipe稍后在按下“添加配方按钮”时使用,因为它在这两种方法中都使用,所以据我所知,对象currentR

请记住,这个问题可能是初学者错误的结果

在我的程序中有4个类与这个问题相关

主要形式:直接在类中声明并启动对象currentRecipe(使用类recipe)和recipeMngr(使用类recipeManager)。 currentRecipe(Recipe)有两个从用户输入中收集的字段,这些字段包括数组成分[],该数组成分[]是从按下按钮时打开的另一种形式的属性中收集的。currentRecipe稍后在按下“添加配方按钮”时使用,因为它在这两种方法中都使用,所以据我所知,对象currentRecipe需要在这些方法之外初始化

RecipeManager:保存存储配方的数组。以及管理将配方添加到数组中的方法。此方法将currentRecipe作为主窗体的属性

配方:保存配方的模板

形成成分:从用户处收集成分并将其存储在属性中

然而,问题仍然存在。将配方存储在recipeManager的数组中时,最新存储的数组只是复制,因此列表中所有先前分配的项都会获得新值。结果

配方[0]=华夫饼

配方[1]=

当添加新的时,它将变为

配方[0]=馅饼

食谱[1]=馅饼

什么时候应该

配方[0]=华夫饼

食谱[1]=馅饼

我已经从主窗体中添加了最重要的部分,从中可以知道问题的来源

我想补充一点,当将currentRecipe的初始化移到“addRecipe click”方法时,问题就消失了。但是这会产生很多新问题,代码的结构应该是这样的

我不使用任何循环来填充数组。

说明: 类是引用类型。如果您浏览了发布的原始代码,currentRecipe只实例化一次。每次更改scurrentRecipe/currentcipe变量的值时,您只需更改该对象。如果需要多个对象,则必须使用new关键字多次创建它们

更新代码,让您明白:

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    InitializeGUI();

}

private const int maxRecipe = 20;
private const int maxIngredients = 50;


private static Recipe currentRecipe = new Recipe(maxIngredients);
private static RecipeManager recipeMngr = new RecipeManager(maxRecipe);

private void btnAddIngredients_Click(object sender, EventArgs e)
{

    FormIngredients FormI = new FormIngredients(currentRecipe);

    var result = FormI.ShowDialog();//show ingredient form

    if (result == DialogResult.OK) {
        currentRecipe = FormI.recipe;
        lblIngredAmount.Text = "Ingredients: " + currentRecipe.ingredientsAmounts().ToString();

    }
}


private void AddRecipe() {
    scurrentRecipe = new Recipe(maxIngredients);
    scurrentRecipe.rName = tbxName.Text;
    scurrentRecipe.rDescription = tbxDescription.Text;
    scurrentRecipe.rCategory = (FoodCategories)cbxCategory.SelectedIndex;


    bool valid = checkIfValid();
    if (valid)
    {
        recipeMngr.Add(scurrentRecipe);
        updateGUI();
        SimpleInitializeGUI();
        currentRecipe = scurrentRecipe();
    }
    else
    {
        MessageBox.Show("Please fill all the fields (or add atleast one ingredient)", "Stop");
    }
}

scurrentRecipe
不是当前的配方对象-它是唯一的配方,您正在更改其内容并添加它,就好像它是在错误的副本上新用完了我的duplehammer一样。正确的副本-除了Proputix所说的,RecipeManager的添加方法是什么@AlexeiLevenkov投了你一票,添加scurrentRecipe的可能重复只是我不顾一切的尝试之一,删除它,仅仅使用currentRecipe也不起作用。以前尝试过,但没有意识到c#是如何处理实例的。了解了一些这方面的知识和“深度复制”,并在一段时间后设法解决了这个问题。谢谢你,我很感激!
public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    InitializeGUI();

}

private const int maxRecipe = 20;
private const int maxIngredients = 50;


private static Recipe currentRecipe = new Recipe(maxIngredients);
private static RecipeManager recipeMngr = new RecipeManager(maxRecipe);

private void btnAddIngredients_Click(object sender, EventArgs e)
{

    FormIngredients FormI = new FormIngredients(currentRecipe);

    var result = FormI.ShowDialog();//show ingredient form

    if (result == DialogResult.OK) {
        currentRecipe = FormI.recipe;
        lblIngredAmount.Text = "Ingredients: " + currentRecipe.ingredientsAmounts().ToString();

    }
}


private void AddRecipe() {
    scurrentRecipe = new Recipe(maxIngredients);
    scurrentRecipe.rName = tbxName.Text;
    scurrentRecipe.rDescription = tbxDescription.Text;
    scurrentRecipe.rCategory = (FoodCategories)cbxCategory.SelectedIndex;


    bool valid = checkIfValid();
    if (valid)
    {
        recipeMngr.Add(scurrentRecipe);
        updateGUI();
        SimpleInitializeGUI();
        currentRecipe = scurrentRecipe();
    }
    else
    {
        MessageBox.Show("Please fill all the fields (or add atleast one ingredient)", "Stop");
    }
}