System.IndexOutOfRangeException c#

本文关键字:IndexOutOfRangeException System | 更新日期: 2023-09-27 18:00:29

不知怎么的,这似乎在几年前就奏效了;)还是我错过了什么?

会通知帮助thx。

else if (recStatus == 3)         
{
            ((TextBox)rref[1]).Text = read;
            //string readData = read;
            string[] readData = read.Split(new Char[] { ',' });

            //string[] readdata = read.Split(',');
            txtType.Text = readData[0];
            txtSerno.Text = readData[1]
            txtFirmware.Text = readData[2] + "." + readData[3];
}

System.IndexOutOfRangeException c#

系统。此代码中的IndexOutOfRangeException表示readData的值小于4,

你可以通过知道元素的数量

string[] readData = read.Split(new Char[] { ',' });
MessageBox.Show(readData.Length.ToString()); //you will found it less then 4

我刚刚运行了您的示例,在控制台应用程序中没有出现错误(可能是因为您正在传递的文本,请放入调试器,查看((TextBox)rref[1]上的实际文本)。文本):

class Program
    {
        static void Main(string[] args)
        {
            string read = "A401,192,2,8" ;
            string[] readData = read.Split(new Char[] { ',' });

            //string[] readdata = read.Split(',');
            string a = readData[0];
            string b = readData[1] + "." + readData[2];
            string c = readData[3];
            Console.WriteLine(a + b + c);
        }
    }

结果:

A401192.28
Press any key to continue . . .