我们如何处理索引超出范围的异常 [英] how can we handle index out of range exception
问题描述
变量"can_Count"的值为5,但此代码仍会导致超出范围的异常.此代码的目的是创建许多动态按钮(它不应该创建can_Count的按钮)变量成立.)我找不到这段代码有什么问题.有人可以帮我吗?
the variable "can_Count" is getting the value of 5.but still this code causes for an out of range exception.The purpose of this code is to create a number of dynamic buttons(it should create no of buttons which the can_Count variable holds.) I can''t find what is wrong with this code.can anyone help me?
public static int can_Count = 0;
//there is a function which count the no of rows in a table n return it to can_Count
static int a_size = (can_Count + 1);
public Button[] bu1 = new Button[a_size];
private void Form9_Load(object sender, EventArgs e)
{
x = 40;
for (int i = 1; i <=can_Count; i++)
{
bu1[i] = new Button();
bu1[i].Name = "BI" + i;
bu1[i].Text = "1";
bu1[i].Location = new Point(20, x);
x = x + 75;
bu1[i].Click += new EventHandler(button1_Click);
}
this.Controls.AddRange(bu1);
}
推荐答案
正如Simmons先生所说,这是您错的地方:
As mr Simmons stated, this is where you''re wrong:
for (int i = 1; i <=can_Count; i++)
应该是:
It should be:
for (int i = 1; i < can_count; i++)
您正在使系统超出范围异常,因为您尝试访问的数组中没有数据.
例如,假设您的数组的计数为4.数组中的第一个元素位于array [0]中,最后一个位于array [3]中. array [4]不存在,如果您尝试访问其中的信息,则会使系统超出范围异常.
0,1,2,3实际上是数组中的4个元素
当您为i< = can_Count循环时,应为i< can_Count.
You''re getting the system out of range exception because you''re trying to access data in the array where there is none.
Example, say your array has a count of 4. The first element in the array is located in array[0] and the last in array[3]. array[4] does not exist and if you''re trying to access information in it, you will get the system out of range exception.
0,1,2,3 is actually 4 elements in the array
This happens when your looping for i <=can_Count, it should be i <can_Count.
在.Net中索引从0开始,因此您需要执行此操作(假设已初始化为适当的尺寸):
Indexes are 0-based in .Net, so you need to do this (assumingbu1
is already initialized to the proper dimensions):
for (int i = 1; i < can_count; i++)
{
....
}
编辑
我会这样:
EDIT
I would do it this way:
for (int i = 1; i < bu1.Length; i++)
{
Button button = new Button() {
.Name = string.Format("BI{0}", i),
.Text = i.ToString(),
.Location = new Point(20, x)
}
x += 75;
bu1[i] = button;
}
并请注意,在您的版本中,您正在尝试向字符串添加整数值(在此设置按钮的Name
属性).这也会引发范围异常.
And notice that in your version, you''re trying to add an integer value to a string (where you set the Name
property of the button). That will also throw a range exception.
我不确定我是否正确,但是此代码创建了5个按钮,而没有生成任何异常:
I''am not sure if i get you right, but this code creates 5 buttons without any exception generated:
public static int can_Count = 5;
//there is a function which count the no of rows in a table n return it to can_Count
static int a_size = (can_Count + 1);
public Button[] bu1 = new Button[a_size];
private void button1_Click(object sender, EventArgs e)
{
int x = 40;
for (int i = 1; i <= can_Count; i++)
{
bu1[i] = new Button();
bu1[i].Name = "BI" + i;
bu1[i].Text = "1";
bu1[i].Location = new Point(20, x);
x = x + 75;
}
this.Controls.AddRange(bu1);
}
这篇关于我们如何处理索引超出范围的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!