我写了如下一段代码:
string loadCurrentpath = Directory.GetCurrentDirectory();
string loadpatch = loadCurrentpath + @"\bin\tab.txt";
string loadstrline;
string loadclassname;
FileStream loadfs = new FileStream(loadpatch,FileMode.Open,FileAccess.ReadWrite,FileShare.Read);
StreamReader loadsr = new StreamReader(loadfs,System.Text.Encoding.Default);
loadstrline = loadsr.ReadLine();
char loadtabflag0 = loadstrline[int j=loadstrline.Length-1];
string loadtabflag = loadtabflag0.ToString();
MessageBox.Show(loadtabflag,"提示");
loadclassname = loadstrline.Substring(0,loadstrline.Length-2);
MessageBox.Show(loadclassname,"提示");
MessageBox.Show(loadstrline,"提示");
.................................
messagebox是我用来测试逻辑是否正确的语句
编译执行后 跳出的messagebox显示的都完全正确,但是光标就停在“char loadtabflag0 = loadstrline[int j=loadstrline.Length-1];”这一行,提示未处理的“System.IndexOutOfRangeException”类型的异常出现在 mscorlib.dll 中。其他信息: 索引超出了数组界限请问大虾这是怎么回事?

解决方案 »

  1.   

    应该不用声明变量j吧?直接loadstrline[loadstrline.Length-1];好了
      

  2.   

    使用index之前应该判断
    if (index > 0)
    {
       ...
    }
    loadstrline.Length-2 ,loadstrline.Length-1均属此列
      

  3.   

    char loadtabflag0 = loadstrline[int j=loadstrline.Length-1];这一句拷错了
    该是char loadtabflag0 = loadstrline[loadstrline.Length-1];
      

  4.   

    CatherineW你说的我不明白 没用index
      

  5.   

    1。将 int j=loadstrline.Length-1 改为loadstrline.Length-1 
    2。查看loadstrline.Length-1 的值,是否超出的数组的长度
      

  6.   

    已经申明拷贝错了 
    loadstrline.Length-1怎么可能超出 读出的loadstrline不是空的
      

  7.   


    if(loadstrline.Length>0)
    {
    }
      

  8.   

    chenyuming2004(这辈子我算是废了) 我读的东西肯定是有的 loadstrline.Length肯定大于0
      

  9.   

    chenyuming2004(这辈子我算是废了) 我读的东西肯定是有的 loadstrline.Length肯定大于0