指数超出范围 [英] Index Out of Range exveption

查看:116
本文介绍了指数超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序抛出索引超出范围异常我知道这个异常的原因假设...

my Programme is throwing Index out of range exception i know the reason for this exception suppose ...

int [] array = new int [100]
array[0] =1;
array[10] = 2;
array[200] = 3;



在这种情况下,它抛出异常清楚,

.....这是我现在的代码


In such a case it throws exception that is clear,
.....Here is my code now

bool[] arraySection = new bool[control.Width, control.Height];
for (int a = 0; i < control.Width; i++)
{
   for (int b= 0; j < control.Height; j++)
   {
      array[a, b] = false;
   }
}

foreach (Data oData in control.cData)
{
   for (int a = oData.Left - control.Left; a < 
            ((oData.Left + oData.Width) - control.Left); a++)
   {
      for (int b = oData.Top - control.Top; j <  
            ((oData.Top + oData.Height) - control.Top); b++)
      {
         arrayS[a, b] ;
      }
   }
}



我无法弄明白为什么????任何人都有解决方案


I am not able to figure out why????Anyone got solution

推荐答案

嗯。

看看你在做什么:

Um.
Look at what you are doing:
int [] array = new int [100];

将数组声明为100个整数的单位维数组。

Declares array as a signle dimensional array of 100 integers.

array[200] = 3;

尝试访问具有100个元素的数组的元素201。 ..



但这不会编译:

Tries to access element 201 of an array with 100 elements...

But that won't compile:

array[a, b] = false;

数组不是二维数组,它是一维的。而且C#严格要求这样做 - 非常正确!



数组不是bool类型 - 它是整数。并且你不能将bool存储在整数位置!

array is not a two dimensional array, it's one dimensional. And C# is strict about such things - quite rightly!

And array isn't of type bool - it's integer. And you can't store a bool in an integer location!


它只是意味着

It simply means that
((oRange.Left + oRange.Width) - control.Left) > control.With






or

((oData.Top + oData.Height) - control.Top) > control.Height





现在由你决定为什么你的代码属于这种边缘情况。

最好的选择是使用调试器和在执行时注意相关变量的值。





代码的第二部分还有4个大错字你提供了(你在所有循环中交换了变量名a / i和b / j)。

实际上它甚至不会编译,所以我想这只是你在写问题时犯的错误。

[/编辑]



Now it's up to you to find why your code falls into this edge-case.
Best option is to use the debugger and watch for the values of relevant variables while executing.


There are also 4 big typos in the second part of the code you provided (you swapped variable names a/i and b/j in all you for loops).
Actually it would not even compile, so I guess it's just a typo you made while writing your question.
[/Edit]


这篇关于指数超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆