指数数组的边界之外。 [英] Index was outside the bounds of the array.

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

问题描述

全部好,



我有一个关于Index的错误超出了数组的范围。



这里是错误代码:









Line 21:if(e.CommandName ==view)

第22行:{

第23行:Response.Redirect(〜/ ViewDetails.aspx?val = + ListView1.DataKeyNames [e.Item.DataItemIndex] .ToString());

第24行:}

第25行:}

解决方案

我们不能绝对解决这个问题 - 很明显你会得到错误: e.Item.DataItemIndex 是负数还是大于 - 或等于中的项目数ListView1.DataKeyNames



但是它为什么会失败给你!使用调试器:在行上放置一个断点并运行代码。当断点命中时,查看变量并查看您拥有的值以及数组的大小。然后你可以开始向后工作以找出原因 - 但我们不能因为我们无法访问你的数据!


它是说e.Item的值。 DataItemIndex大于ListView1.DataKeyNames数组的上限索引。



例如,如果ListView1.DataKeyNames的大小为7,则其合法索引将为0, 1,2,... 6.



如果e.Item.DataItemIndex为7则会抛出此错误。



试试这样..



您正在尝试访问不可用的集合的索引值,因此最好检查可用的索引访问之前。



  int  index = e.Item.DataItemIndex; 
int length = ListView1.DataKeyNames.Length;
if (index < length)
Response.Redirect( 〜/ ViewDetails.aspx?val = + ListView1.DataKeyNames [index] .ToString());


HI ALL,

I have got an error on Index was outside the bounds of the array.

here is the error code:




Line 21: if (e.CommandName == "view")
Line 22: {
Line 23: Response.Redirect("~/ViewDetails.aspx?val=" + ListView1.DataKeyNames[e.Item.DataItemIndex].ToString());
Line 24: }
Line 25: }

解决方案

We can't absolutely solve that for you - it's obvious why you get the error: e.Item.DataItemIndex is negative or greater-than-or-equal-to than the number of items in ListView1.DataKeyNames

But why it is it down to you! Use the debugger: put a breakpoint on the line and run your code. when the breakpoint hits, look at the variables and see what value you have, and how big the array is. Then you can start to work backwards to find out why - but we can't because we don't have access to your data!


It is saying that the value of e.Item.DataItemIndex is greater than the upper bound index of ListView1.DataKeyNames array.

for example if the size of ListView1.DataKeyNames is 7, then its legal indexes will be 0,1,2,...6.

if e.Item.DataItemIndex is 7 then this error will be thrown.


Hi Try like this..

You are trying to access the index value of collection which is not available, so its better to check the index available before accessing.

int index = e.Item.DataItemIndex;
              int length = ListView1.DataKeyNames.Length;
              if( index < length)
                  Response.Redirect("~/ViewDetails.aspx?val=" + ListView1.DataKeyNames[index].ToString());


这篇关于指数数组的边界之外。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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