指数超出范围

编程入门 行业动态 更新时间:2024-10-25 16:27:10
本文介绍了指数超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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]

更多推荐

指数超出范围

本文发布于:2023-10-16 12:18:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!