我收到了错误(索引超出了数组的范围) [英] I got the error (index was outside the bounds of the array)

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

问题描述

我想使用字符文字和数字自动生成代码(SH / OPD / 2015/01),应该是自动增量,PLZ帮助我。我的代码是







  string  NewCode =  ; 
string str = ;
str = 选择Opdid From temp;
str = 从opd_reg中选择MAX(Opdid)AS'Oddid';
DataSet ds = new DataSet();
ds = c.ExecuteSQL(str);
if (ds.Tables [ 0 ]。Rows.Count > 0
{
string maxMainCode = ds.Tables [ 0 ]。行[ 0 ] [ Opdid]。ToString();
// int NewCount = Convert.ToInt32(Code.Substring(2))+ 1;
if (maxMainCode!=
{
string [] arInfo = new string [ 150 ];
char [] splitter1 = {' S '};
char [] splitter2 = {' H '};
char [] splitter3 = {' / '};

char [] splitter4 = {' O'};
char [] splitter5 = {' P '};
char [] splitter6 = {' D '};
char [] splitter7 = {' / '};
char [] splitter8 = {' 2 '};
char [] splitter9 = {' 0 '};
char [] splitter10 = {' 1 '};
char [] splitter11 = {' 5 '};
char [] splitter12 = {' / '};

arInfo = maxMainCode.Split(splitter1);
arInfo = maxMainCode.Split(splitter2);
arInfo = maxMainCode.Split(splitter3);
arInfo = maxMainCode.Split(splitter4);
arInfo = maxMainCode.Split(splitter5);
arInfo = maxMainCode.Split(splitter6);
arInfo = maxMainCode.Split(splitter7);
arInfo = maxMainCode.Split(splitter8);
arInfo = maxMainCode.Split(splitter9);
arInfo = maxMainCode.Split(splitter10);
arInfo = maxMainCode.Split(splitter11);
arInfo = maxMainCode.Split(splitter12);

int OrignalNum = Convert.ToInt32(arInfo [ 180 ]。ToString( ));
int 添加;
Add = OrignalNum + 10 ;
if (Convert.ToString(OrignalNum).Length == Convert.ToString(Add).Length)
{
NewCode = maxMainCode.Replace(OrignalNum.ToString(),Add.ToString());
}
else
{
NewCode = maxMainCode.Replace( 0 + OrignalNum.ToString(),Add.ToString());
}
}
else
{
NewCode = SH / OPD / 2015/01;

}
}
else
{
NewCode = SH / OPD / 2015/01;
objs.Stmt = 插入临时(Opdid)值(' + NewCode + ');
objs.CallQuery(objs);
}
返回 NewCode;
}

解决方案

阵列中很可能没有180个元素。你似乎依赖于maxMainCode的长度和结构。



在我看来,我不会继续使用这段代码,我会考虑是否会有更好的方法来做到这一点。你定义数组,但实际上并没有使用它们(分离器),为什么?另一方面,如果数据不包含您列出的字符,该怎么办?



数据库获取语句Select Opdid From temp从未执行,因为它被另一个语句取代。我不知道这是否是故意的......



我建议不要一次性解决所有问题,而是建议将问题分解成小块,一个接一个地解决它们。最有可能的是,大局也变得正确。 :)

i want to auto generate code using character literals and digit like this (SH/OPD/2015/01),Is should be auto increment,plz help me.My code is



string NewCode = "";
           string str = "";
           str = "Select Opdid From temp";
           str = "Select MAX(Opdid) AS 'Opdid' from opd_reg";
           DataSet ds = new DataSet();
           ds = c.ExecuteSQL(str);
           if (ds.Tables[0].Rows.Count > 0)
           {
               string maxMainCode = ds.Tables[0].Rows[0]["Opdid"].ToString();
               // int NewCount = Convert.ToInt32(Code.Substring(2))+1;
               if (maxMainCode != "")
               {
                   string[] arInfo = new string[150];
                   char[] splitter1 = { 'S' };
                   char[] splitter2 = { 'H' };
                   char[] splitter3 = { '/' };

                   char[] splitter4 = { 'O' };
                   char[] splitter5 = { 'P' };
                   char[] splitter6 = { 'D' };
                   char[] splitter7 = { '/' };
                   char[] splitter8 = { '2' };
                   char[] splitter9 = { '0' };
                   char[] splitter10 = { '1' };
                   char[] splitter11 = { '5' };
                   char[] splitter12 = { '/' };

                   arInfo = maxMainCode.Split(splitter1);
                   arInfo = maxMainCode.Split(splitter2);
                   arInfo = maxMainCode.Split(splitter3);
                   arInfo = maxMainCode.Split(splitter4);
                   arInfo = maxMainCode.Split(splitter5);
                   arInfo = maxMainCode.Split(splitter6);
                   arInfo = maxMainCode.Split(splitter7);
                   arInfo = maxMainCode.Split(splitter8);
                   arInfo = maxMainCode.Split(splitter9);
                   arInfo = maxMainCode.Split(splitter10);
                   arInfo = maxMainCode.Split(splitter11);
                   arInfo = maxMainCode.Split(splitter12);

                   int OrignalNum = Convert.ToInt32(arInfo[180].ToString());
                   int Add;
                   Add = OrignalNum + 10;
                   if (Convert.ToString(OrignalNum).Length == Convert.ToString(Add).Length)
                   {
                       NewCode = maxMainCode.Replace(OrignalNum.ToString(), Add.ToString());
                   }
                   else
                   {
                       NewCode = maxMainCode.Replace("0" + OrignalNum.ToString(), Add.ToString());
                   }
               }
               else
               {
                   NewCode = "SH/OPD/2015/01";

               }
           }
           else
           {
               NewCode = "SH/OPD/2015/01";
               objs.Stmt = "Insert Into temp (Opdid) values ('" + NewCode + "')";
               objs.CallQuery(objs);
           }
           return NewCode;
       }

解决方案

Most likely don't have 180 elements in your array. Also you seem to rely on the length and structure of the maxMainCode.

In my opinion, instead of going on with this code, I'd consider if there would be better ways to do this. You define arrays but you don't actually use them (splitters), why? On the other hand what if the data doesn't contain the characters you've listed.

What comes to the database fetch the statement "Select Opdid From temp" is never executed since it's replaced by another statement. I don't know if this is intentional or not...

Instead of trying to solve all things at once, I would suggest breaking the problem to small pieces and solving them one-by-one. Most likely this way the big picture also becomes correct. :)


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

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