C# 我有;索引超出了数组的边界;错误

C# 我有;索引超出了数组的边界;错误,c#,C#,单击按钮3和按钮4时出现问题 我不知道我做错了什么 看起来我的程序试图将threadArray置于索引之外的位置 ps我的英语不好,但我能读得很好 Thread[] threadArray; int numberThread; public delegate void myDelegate(int x); myDelegate[] myDelegates; int[] numberOfIterations; public Form1()

单击按钮3和按钮4时出现问题 我不知道我做错了什么 看起来我的程序试图将threadArray置于索引之外的位置

ps我的英语不好,但我能读得很好

    Thread[] threadArray;
    int numberThread;

    public delegate void myDelegate(int x);
    myDelegate[] myDelegates;

    int[] numberOfIterations;

    public Form1()
    {
        InitializeComponent();
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox1.SelectedIndex = 0;
        comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
        comboBox2.Enabled = false;
        comboBox3.Enabled = false;
        numericUpDown2.Enabled = false;
        comboBox3.Items.Add("product");
        comboBox3.SelectedIndex = 0;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        threadArray = new Thread[(int)numericUpDown1.Value];
        numberThread = (int)numericUpDown1.Value;
        myDelegates = new myDelegate[(int)numericUpDown1.Value];
        numericUpDown1.Enabled = false;
        numberOfIterations = new int[numberThread];
        for (int i = 0; i < numberThread; i++)
        {
            comboBox2.Items.Add(i+1);
        }
        comboBox2.Enabled = true;
        button1.Enabled = false;
    }

    private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox3.Enabled = true;
        numericUpDown2.Enabled = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        int x = Convert.ToInt32(comboBox2.SelectedItem);
        x = x - 1;
        numberOfIterations[x] = (int)numericUpDown2.Value;
        if (comboBox3.SelectedIndex == 0)
        {
            myDelegates[x] = null;
            myDelegates[x] += Functions.productDev;
        }
        threadArray[x] = new Thread(()=>myDelegates[x]((int)numericUpDown2.Value));
    }

    private void button4_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < numberThread; i++)
        {
            threadArray[i].Start();
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        for (int j = 0; j < numberThread; j++)
        {
            numberOfIterations[j] = (int)numericUpDown2.Value;
            if (comboBox3.SelectedIndex == 0)
            {
                myDelegates[j] = null;
                myDelegates[j] += Functions.productDev;
            }
            threadArray[j] = new Thread(() => myDelegates[j](numberOfIterations[j]));
        }
    }

}
Thread[]线程数组;
整数读取;
公共委托无效myDelegate(int x);
我的代表[]我的代表;
int[]迭代次数;
公共表格1()
{
初始化组件();
comboBox1.DropDownStyle=ComboBoxStyle.DropDownList;
comboBox1.SelectedIndex=0;
comboBox2.DropDownStyle=ComboBoxStyle.DropDownList;
comboBox2.Enabled=false;
comboBox3.Enabled=false;
numericUpDown2.Enabled=false;
组合框3.项目。添加(“产品”);
comboBox3.SelectedIndex=0;
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
threadArray=新线程[(int)numericUpDown1.Value];
numberThread=(int)numericUpDown1.Value;
myDelegates=新的myDelegate[(int)numericUpDown1.Value];
numericUpDown1.Enabled=false;
numberOfIterations=新整数[numberThread];
对于(int i=0;imyDelegates[x]((int)numericUpDown2.Value));
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
对于(int i=0;imyDelegates[j](numberOfIterations[j]);
}
}
}

}我认为问题在于所有数组都在
按钮1\u单击事件中初始化。如果您没有按
按钮1
,该怎么办。而是在构造函数中初始化它们

public void InitValues()
{
    threadArray = new Thread[(int)numericUpDown1.Value];
    numberThread = (int)numericUpDown1.Value;
    myDelegates = new myDelegate[(int)numericUpDown1.Value];
    numericUpDown1.Enabled = false;
    numberOfIterations = new int[numberThread];
}
在构造函数中调用此函数,并从
按钮1\u单击事件中删除此代码和平

public Form1()
{
    //..............
    InitValues();
}

这意味着您正试图访问超出其长度的位置上的数组元素。此外,您还需要解释您试图实现的目标以及该问题发生在哪里。不要期望你会粘贴一堵代码墙,我们将开始搜索你的问题。为什么不尝试附加调试器呢?按钮3\u单击生成长度等于数字的threadArray读取此数组的所有元素已加入委托下一步,当我单击按钮4时,它尝试调用所有threadArray元素,并且出现错误,可以吗解释流程?放一个断点并调试代码,你可以很容易地找到哪一行抛出异常。数组不好,我试图用列表重写程序