索引超出范围异常的数组 [英] Index out of range exception for array

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

问题描述

 if(coupleSubstrings.Count> 1)
{
for(int i = 0; i< coupleSubstrings.Count-1; i ++)
coupleTemp [ i] = coupleSubstrings [i] ++ coupleSubstrings [i + 1];

dt.Clear();
if(!ViewReqInfo(dt,coupleTemp.ToArray()))
{
lblErrorMessage.CurrentValue = objWord.ErrMessage;
}
if(dt.Rows.Count> 0)
{
foreach(dt.Rows中的DataRow dr)
{
value = dr [ IsInsult]的ToString();
if(value ==True)
{
lblErrorMessage.CurrentValue = string.Empty;
lblErrorMessage.CurrentValue =اینجملهشاملکلماتغیرمجازمیباشد。;
休息;
}
else
{
lblErrorMessage.CurrentValue = string.Empty;
lblErrorMessage.CurrentValue =اینجملهنیازمندبررسیبیشتراست。;
txtEntry.CurrentValue = string.Join(,coupleTemp);
}
}
}
}
}





我尝试了什么:



此方法不起作用,数组长度更长但我不知道问题是什么

解决方案

查看你的代码:

 for(int i = 0; i< coupleSubstrings.Count-1; i ++ )
coupleTemp [i] = coupleSubstrings [i] ++ coupleSubstrings [i + 1];

循环运行i到数组中的最后一个元素,但是你可以访问该元素加一个 - 根据定义它不存在。


问题必须在于 coupleTemp 数组,因为 coupleSubstrings 一个被正确编入索引。你介意显示 coupleTemp 数组的声明吗?看来你没有为它提供足够的元素(理想情况下,它应该有一个元素小于 coupleSubstrings 数组)。


< blockquote class =quote>

Quote:

索引超出范围的异常



除了错误信息,你还有告诉错误的位置,分享会很高兴。由于代码片段不是自治的,因此很难猜出是什么。

引用:

此方法不具备工作数组长度更长,但我不知道是什么问题



由于与计算机争论你的代码是正确的将无济于事,你必须找到另一种方法。另一种方法是使用调试器运行代码,并在发生错误时检查变量和数组,以了解您的代码正在做什么以及为什么计算机拒绝它。



那里是一个工具,可以让您查看代码正在做什么,其名称是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - 您管 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它找不到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


if (coupleSubstrings.Count > 1)
      {
          for (int i = 0; i<coupleSubstrings.Count-1; i++)
              coupleTemp[i] = coupleSubstrings[i] +" "+ coupleSubstrings[i + 1];

          dt.Clear();
          if (!ViewReqInfo(dt, coupleTemp.ToArray()))
          {
              lblErrorMessage.CurrentValue = objWord.ErrMessage;
          }
          if (dt.Rows.Count > 0)
          {
              foreach (DataRow dr in dt.Rows)
              {
                  value = dr["IsInsult"].ToString();
                  if (value == "True")
                  {
                      lblErrorMessage.CurrentValue = string.Empty;
                      lblErrorMessage.CurrentValue = "این جمله شامل کلمات غیر مجاز می باشد.";
                      break;
                  }
                  else
                  {
                      lblErrorMessage.CurrentValue = string.Empty;
                      lblErrorMessage.CurrentValue = "این جمله نیازمند بررسی بیشتر است.";
                      txtEntry.CurrentValue = string.Join(" ", coupleTemp);
                  }
              }
          }
      }
  }



What I have tried:

This method does not work the array length is longer but I dont know what is the problem

解决方案

Look at your code:

for (int i = 0; i<coupleSubstrings.Count-1; i++)
          coupleTemp[i] = coupleSubstrings[i] +" "+ coupleSubstrings[i + 1];

The loop runs i from to the last element in the array, but you then access that element plus one - which by definition does not exist.


The problem has to be with the coupleTemp array, since the coupleSubstrings one is properly indexed. Would you mind showing the declaration of the coupleTemp array? It appears you did not provide it with enough elements (ideally, it should have one element less than the coupleSubstrings array).


Quote:

Index out of range exception for array


Along with the error message, you have also been told the position of error, would be nice to share. Since code snipset is not autonomous, it is difficult to guess what is what.

Quote:

This method does not work the array length is longer but I dont know what is the problem


Since arguing with computer that your code is right will not help, you have to find another way. This other way is to run your code with debugger, and when error occurs, inspect variables and array to understand what your code is doing and why computer reject it.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


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

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