c# - 如何在类数组中设置类值

标签 c# arrays indexoutofboundsexception

我正在使用 Visual Studio 创建一个 Windows 窗体 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;
        }
    }
}

最佳答案

所报告异常的问题[很可能]是 accounts[acctcount-1] 行,因为当 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在账户类中;否则成员(member)数据将被错误地共享(即每个帐户将具有相同的余额!)并相互“覆盖”。如果使用 static 是试图从表单“传回”数据,请参见 How to return a value from a Form in C#?以获得更可行的解决方案。公共(public)属性的相同用法可用于将 (Account) 对象传递到表单。

关于c# - 如何在类数组中设置类值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26079323/

相关文章:

c# - 无法更新图片框内的图像

java - 获取 ArrayList 的值时出现意外的 IndexOutOfBoundException

java - 将具有不均匀大小的排序行的二维数组合并为排序的一维数组

php - 使用 PHP 将 javascript 数组添加到数据库的想法

初始化数组对象时出现java.lang.ArrayIndexOutOfBoundsException

c# - 启动新线程时抛出IndexOutOfRangeException

c# - 使用 NPOI 时出现无法理解的错误

c# - 如何等待 task.run

c# - 使用 C# 和 ASP.NET 从 Gmail/Hotmail/Yahoo 导入地址簿

javascript - 从 JSON 数组中删除元素