2017-07-28 76 views
0

我創建了一個函數(如下所示),將9個按鈕的文本全部分配給「」(我通常嘗試使用「」,而不是將一個變量分配給某個不合邏輯的假設,所以如果我不正確,我將不勝感激),但我覺得我可以用更簡潔的方式將每個參數分配給空白字符串。C#乾淨地爲每個方法參數分配一個值

public void resetText(ref string buttonText1, ref string buttonText2, ref string buttonText3, ref string buttonText4, ref string buttonText5, ref string buttonText6, ref string buttonText7, ref string buttonText8, ref string buttonText9) 
    { 
     buttonText1 = ""; 
     buttonText2 = ""; 
     buttonText3 = ""; 
     buttonText4 = ""; 
     buttonText5 = ""; 
     buttonText6 = ""; 
     buttonText7 = ""; 
     buttonText8 = ""; 
     buttonText9 = ""; 
    } 

是否有更簡潔的方法來重新分配每個參數。如果我不得不在這個函數中爲50個變量賦一個新的值(這裏我只是使用了按鈕作爲例子),這看起來相當糟糕。)

+0

似乎沒有更乾淨的方法來做到這一點。但是,也許父函數可以通過將文本值保存在數組中來更好地實現這一點。 –

+0

因此,你不知道只是循環遍歷所有參數併爲它們分配一個空值或類似的東西? –

+0

可以更新爲:'buttonText1 = buttonText2 = buttonText3 = buttonText4 = buttonText5 = buttonText6 = buttonText7 = buttonText8 = buttonText9 = string.Empty; ' – zzT

回答

0

這是非常好的方法。它簡單易讀。

如果你真的想要減少字符數,你可以這樣做buttonText1 = buttonText2 = buttonText3 = ... = "";

減少代碼大小的另一種方法是通過反射來完成。我不會推薦這樣做,因爲它使代碼過於複雜,只能保存幾個字符。