C#索引超出范围 [英] C# Index Out Of Range

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

问题描述

嗨!

我正在检查两个datagridviews之间的值,如果其中之一不存在该值,则需要插入到value中.我的代码工作正常,但是当我获得不存在的值时,我得到了索引超出范围错误.

我知道错误的含义,但我不知道如何处理.在执行代码之前,我需要检查索引是否超出范围.

有人可以告诉我如何检查索引是否超出范围吗? ,毫无用处,绝对无用..."绝对不会使任何人想要帮助您.

其次,是的,买一本书.从编码的基础开始,因为很明显您还没有这样做.从这个问题来看,我只能假设您已经做了类似的事情:

  int  i =  0 ;

同时()
{
  如果(dataGridView1.Rows(i)...)
  {

  }
  i ++;
} 



这根本不是正确的方法.

几乎所有控制列表的内容都将具有Count属性.告诉您...等待中...该列表中的项目数.那么,如何:

  int  i =  0 ;

同时(i <  dataGridView1.Rows.Count)
{
  // 做某事

  i ++;
} 



当然,这是从基本编码类开始的解决方案.有很多其他方法可以做到,如果您不了解它们,那就买本书或上课.


我建​​议您购买一本基本的编程书籍并阅读. ,并在ASP.NET之前学习基本编码.但是,我认为这不太可能发生.

检查集合中的对象数,以确保要请求的索引存在.通常,有一个Count属性.


要检查索引是否超出范围,可以使用if语句来检查您要针对您要枚举的集合的计数"属性.数组使用长度"属性而不是计数".当您尝试访问超出范围的索引时,您还可能捕获引发的异常.

但是,为什么您可能要问自己,为什么要尝试访问超出范围的索引.如果使用的是for循环,则应将0用作下限,将Count-1用作上限(对于数组,则为Length-1).如果使用的是while循环,则退出的条件应包括您可能找不到所需的内容,并应在超出可能的索引范围之前退出循环.

这是一个基本概念,也是CG建议您拿书的原因.他的回答很好,但似乎您听不懂.也许如果您发布一些代码,我们可以为您提供更具体的帮助,但是在此之前,您将仅获得非特定的答案.仅供参考,这是一个如何导致IndexOutOfRangeException的示例.

 字符串 []字符串=   1 ];
字符串[ 1 ] = "  ; //  IndexOutOfRangeException. 


修复应该很明显(即,不要访问索引1,因为索引0是您可以在示例数组上访问的唯一索引).


Hi!

I am checking values between two datagridviews and if the value does not exist in one of them I need to insert to value. My code works fine but when I get to the value that does not exist I get an Index out of range error.

I know what the error means but I do not know how to handle it. I need to check if the index is out of range before executing my code.

Can someone please tell me how to check if the Index is out of range?

解决方案

So, first, when people see you responding to a perfectly valid answer with stuff like, "useless, absolutely useless..." it definitely doesn''t make anyone want to help you.

Secondly, yes, buy a book. Start with the basics of coding, because it''s clear you haven''t done that. From the question, I can only assume that you''ve done something like:

int i = 0;

while (true)
{
  if (dataGridView1.Rows(i)...)
  {

  }
  i++;
}



which would not be the correct way to do it at all.

Pretty much anything that controls a list will have a Count property. This tells you...wait for it...the number of items in that list. So, how about:

int i = 0;

while(i < dataGridView1.Rows.Count)
{
  //do something

  i++;
}



of course, that is the solution that you start with in a basic coding class. There are many, many other ways to do it and if you don''t know them, then buy a book or find a class.


I suggest buying a basic programming book and reading it, and learning basic coding BEFORE ASP.NET. But, I see that''s not likely to happen.

Check the number of objects in a collection to make sure the index you''re about to request, exists. There''s a Count property, typically.


To check if an index is out of range, you could use an if-statement to check the current index you are trying to access against the "Count" property of the collection you are enumerating. An array uses the "Length" property rather than "Count". You could also catch the exception thrown when you attempt to access an index that is out of range.

However, why you should probably be asking yourself why you are attempting to access an index that is out of range. If you are using a for-loop, you should be using 0 as the lower bound and Count - 1 as the upper bound (in the case of an array, that would be Length - 1). If you are using a while-loop, your condition for exiting should include the possibility that you may not find what you are looking for and should exit the loop before you exceed the possible range of indexes.

This is a basic concept and the reason CG suggested you get a book. His answer was fine, but it seems you didn''t understand it. Perhaps if you post some code we can help you more specifically, but until then you''ll just get non-specific answers. FYI, here is an example of how to cause an IndexOutOfRangeException.

String[] strings = new String[1];
strings[1] = "something"; // IndexOutOfRangeException.


The fix should be obvious (i.e., don''t access index 1, as index 0 is the only index you can access on the example array).


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

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