2017-01-30 44 views
0

請記住,此問題可能是初學者錯誤的後果。將值分配給數組時,所有值都相同

我的程序中有4個類與這個問題相關。

主要形式:直接在類中聲明和啓動對象currentRecipe(使用class recipe)和recipeMngr(使用class recipeManager)。 currentRecipe(Recipe)有幾個從用戶輸入中收集的字段,這些字段包含從屬性中收集的另一種形式的數組成分[],該數據按鈕被按下時會打開。 currentRecipe稍後會在按下「添加配方按鈕」時使用,並且因爲在這兩種方法中都使用了currentRecipe,所以需要在這些方法之外初始化對象currentRecipe,據我瞭解。

RecipeManager:保存一個存儲食譜的數組。以及管理將配方添加到數組中的方法。此方法將currentRecipe作爲主窗體的屬性。

食譜:保存食譜的模板。

FormIngredients:從用戶收集成分,並將它們存儲在財產中。

但是,問題所在。當在recipeManager中將配方存儲到數組中時,最新存儲的數組僅複製,以便列表中所有先前分配的項目都獲得新值。作爲一個結果

配方[0] =維夫餅乾

配方[1] =

...

添加新變得

配方[0] =餡餅

配方[1] =餡餅 ...

當它應該成爲

食譜[0] =維夫餅乾

食譜[1] =餡餅

...

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(Recipe scurrentRecipe) { 
     scurrentRecipe.rName = tbxName.Text; 
     scurrentRecipe.rDescription = tbxDescription.Text; 
     scurrentRecipe.rCategory = (FoodCategories)cbxCategory.SelectedIndex; 


     bool valid = checkIfValid(); 
     if (valid) 
     { 
      recipeMngr.Add(scurrentRecipe); 
      updateGUI(); 
      SimpleInitializeGUI(); 

     } 
     else 
     { 
      MessageBox.Show("Please fill all the fields (or add atleast one ingredient)", "Stop"); 
     } 
    } 

我從主表單中添加的最重要的部分,哪裏知道問題來自哪裏。

我想補充說,將currentRecipe的初始化移動到「addRecipe click」方法時,問題消失。但是這會產生很多新問題,代碼應該像這樣構造。我不使用任何循環來填充數組。

+4

'scurrentRecipe'不是當前配方對象 - 這是*僅*配方和,如果它是新的,你正在改變它的內容,並將其添加 – Plutonix

+0

用完了我的duplehammer錯誤的重複。正確的重複 - http://stackoverflow.com/questions/2156482/why-does-adding-a-new-value-to-list-overwrite-previous-values-in-the-list –

+0

除了Plutonix說什麼,什麼是RecipeManager的添加方法? @AlexeiLevenkov投給你 –

回答

0

說明: 類是引用類型。如果您瀏覽了您發佈的原始代碼,那麼currentRecipe僅實例化一次。每當您更改scurrentRecipe/currentRecipe變量的值時,您只是更改該對象。如果您需要多個對象,則必須使用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"); 
    } 
} 
+0

之前嘗試過,但沒有意識到c#如何處理實例。瞭解了一些關於這一點和「深層複製」,並在一段時間後設法解決這個問題。謝謝,我很感激! – Martin

相關問題