2014-09-27 27 views
1

我正在使用Visual Studio創建Windows Forms C#項目,並試圖設置一個類型類的數組,並讓數組中的條目對應於構造函數字符串爲班級。我正在使用具有變量索引的數組,每次將新的類實例添加到數組時都會增加該數組。如何設置類數組中的類值

我遇到了索引調用超出數組邊界的問題。另外,我不確定是否爲每個實例設置了我的類變量。任何人都可以將我指向正確的方向嗎?下面是我的代碼:

public partial class MainMenu : Form 
{ 
    //int that will be used to alter the index of the array 
    public static int acctcount = 1; 
    //array of class Account 
    Account[] accounts = new Account[acctcount]; 
    public MainMenu() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //check through each element of the array 
     for (int i = 0; i < accounts.Length; i++) 
     { 
      string stringToCheck = textBox1.Text; 
      foreach(Account x in accounts) 
      { 
       //check to see if entered name matches any element in the array 
       if (x.Name == stringToCheck) 
       { 
        //set variables in another form so that we are using the class variables for only that class 
        Variables1.selectedAccount = x.Name; 
        //is this calling the CheckBalance of the instance? 
        Variables1.selectedCheckBalance = Account.CheckBalance; 
        //same thing? 
        Variables1.selectedSaveBalance = Account.SaveBalance; 

        //switch to form 
        AccountMenu acctMenu = new AccountMenu(); 
        this.Hide(); 
        acctMenu.Show(); 
       } 
       else 
       { 
        /*insert new instance of Account 
        the index element should be 0, since acctcount is set to 1 
        and we are subtracting 1 from the acctcount 
        we are using the string from the textbox1.Text as the constructor 
        for the new instance*/ 
        accounts [acctcount-1] = new Account(stringToCheck); 
        //increase the index of the array by 1 
        acctcount += 1; 
       } 
      } 
     } 
    } 

} 
class Account 
{ 
    private string name; 
    public string Name 
    { 
     get 
     { 
      return name; 
     } 
     set 
     { 
      name = value; 
     } 
    } 
    private static int acctNum = 0; 
    public static int AcctNumber 
    { 
     get 
     { 
      return acctNum; 
     } 
     set 
     { 
      acctNum = value; 
     } 
    } 
    //initialize the CheckBalance value to 100.00 
    private static decimal checkBalance = 100.00M; 
    public static decimal CheckBalance 
    { 
     get 
     { 
      return checkBalance; 
     } 
     set 
     { 
      checkBalance = value; 
     } 
    } 
    public Account(string Name) 
    { 
     this.Name = Name; 
    } 
    private static decimal saveBalance = 100.00M; 
    public static decimal SaveBalance 
    { 
     get 
     { 
      return saveBalance; 
     } 
     set 
     { 
      saveBalance = value; 
     } 
    } 
} 
+0

另外,您可能希望「Account」類中的屬性爲實例屬性,而不是靜態屬性... – 2014-09-27 21:25:12

+0

是否有您使用數組而不是List的原因? – 2014-09-27 21:44:34

回答

3

與所報告的異常的問題是[最有可能]的線accounts[acctcount-1]因爲它會引發IndexOutOfBounds異常時acctcount是> = 2,所述第一後如發生(例如accounts[1]。)按鈕點擊並增加acctcount。然而,陣列只有有一個元素,因爲它是用accounts = new Account[acctcount]; - 數組在C#中做的而不是長大/調整大小

最簡單和最好的即時修復方法是使用List(請參閱Collections (C#))而不是數組;列表可以動態增長。然後代碼變爲:

// (remove the "acctcount" field as it is no longer useful) 
List<Account> accounts = new List<Account>(); 
// .. 
accounts.Add(new Account(stringToCheck)); 

正如指出由Trevor,除去static modifier在帳戶類;否則會員資料將被錯誤地分享(即每個賬戶將有相同的餘額!)並且「相互覆蓋」。如果使用靜態是試圖「回傳」表單中的數據,請參閱How to return a value from a Form in C#?以獲得更可行的解決方案。可以使用公共財產的相同用途將(帳戶)對象傳遞給表單。

+0

謝謝。切換到列表是有道理的。或者,不能使用array.Resize來代替?對於靜態修改器,我使用它來設置Variables1屬性。不過,我明白你在說什麼。我如何根據數組元素訪問實例的變量? – 2014-09-27 21:41:36

+0

@JustinDay Arrays.Resize創建一個* new *數組。它使用'ref'關鍵字來重新分配作爲輸入提供的變量。我不建議在這裏使用這種方法,雖然有一些用於數組的用例。 – user2864740 2014-09-27 22:00:08

+0

@JustinDay只要有權訪問數據,提供* actual * Account對象進行修改。例如,在表單中修改帳戶信息有一個公共帳戶屬性。將此設置爲該帳戶,以便在顯示錶單之前修改*,然後從表單內修改*該對象*(如分配給表單的帳戶屬性)。 – user2864740 2014-09-27 22:02:12

2

當按鈕被單擊多次時引發異常。

您創建了一個大小爲1的數組,第二次單擊該按鈕並嘗試在索引2處添加元素時,索引已超出邊界。

隨着添加新項目,數組的大小不會增大。

正如指出的那樣,你應該使用一個集合,像List<T>

如果你想使用數組保留,每次你添加一個新的項目,你需要創建更大尺寸的新數組,複製的元素將舊數組添加到新數組,並將舊數組引用到新數組。你也可以創建一個更大尺寸的數組,並且只有在數組滿了時才創建一個新數組。這基本上是.Net集合已經實現的。

一如既往,這一切都取決於您的需求和要求。

相關問題