索引在修剪文本处超出数组边界

Index was outside the bounds of array at trimmed text

我创建了一个连接到数据库的 Windows 表单应用程序,用户可以在其中输入特定的值。我现在面临的问题是我创建了一个搜索,它就像魅力一样。但是,用户总是会按姓名搜索,我希望 trim 这种搜索更加准确。它有效,但是当我只输入姓名或姓氏时,我想显示一个 massageBox 来说明他们没有正确输入。所以我写了这个,但是如果我只输入一个词,它就会崩溃并显示这个错误:索引超出了数组的范围。谢谢!

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text.Length != 0)
    {
        var numePrenume = textBox1.Text.Trim().Split(' ');
        var nume = numePrenume[0];
        var prenume = numePrenume[1];

    if (nume.Length > 0 || prenume.Length > 0)
    {
        var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
        using (var conn = new SqlCeConnection(connString))
        {
        }
    }

    //some code
}

如果你只输入一个单词,那么这一行会导致异常:

var prenume = numePrenume[1];

因为不存在

您需要在尝试访问该数组元素之前添加一些边界检查,例如

if(numePrenume.Length == 2)
{
    var prenume = numePrenume[1];
}

您可以通过验证 Textbox.Text 来尝试,如果您需要填写姓名和姓氏,您可以 return 验证错误。 这可以按如下方式完成:

if(numePrenume.Lenght()==0)
{
   MessageBox.Show("");
}

 return;

希望对您有所帮助:)

检查您的数组长度将解决您的问题:

 if (textBox1.Text.Length != 0)
            {

                var numePrenume = textBox1.Text.Trim().Split(' ');
                if (numePrenume.Length >= 1)
                {
                    var nume = numePrenume[0];
                    var prenume = numePrenume[1];

                    if (nume.Length > 0 || prenume.Length > 0)
                    {
                        var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
                        using (var conn = new SqlCeConnection(connString))
                        {
                        }
                    }
                }
                else
                    // some code here
            }

您需要检查拆分函数 returns 是否多于一个元素。像这样简单地更新您的代码应该可以解决您的问题:

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text.Length != 0)
    {
        var numePrenume = textBox1.Text.Trim().Split(' ');
        var nume = numePrenume[0];

        if(numePrenume.Length >= 1) 
        {
            var prenume = numePrenume[1];
        }

        if (nume.Length > 0 || prenume.Length > 0)
        {
            var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
            using (var conn = new SqlCeConnection(connString))
            {
            }
        }
        //some code
    }

您需要检查 Split() 返回的数组的 length。此外,如果用户在名称部分之间键入多个空格,最好添加 StringSplitOptions.RemoveEmptyEntries 以避免得到两个以上的部分。

考虑这个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var name = "   name  ";

            var nameParts = name.Trim().Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);

            if (nameParts.Length < 2)
            {
                Console.WriteLine("You've only entered one name");
            }
            else
            {
                Console.WriteLine("First part: {0}", nameParts[0]);
                Console.WriteLine("Second part: {0}", nameParts[1]);
            }
        }
    }
}

您需要检查 numePrenume.Length 是否大于 1。如果小于 1,则向用户显示一些消息。

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text.Length != 0)
    {
        var numePrenume = textBox1.Text.Trim().Split(' ');
        if(numePrenume.Length > 1)
        {
            var nume = numePrenume[0];
            var prenume = numePrenume[1];

            if (nume.Length > 0 || prenume.Length > 0)
            {
                var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
                using (var conn = new SqlCeConnection(connString))
                {
                }
            }
                    //some code
         }
         else
         {
              Console.Writeline("You need to input both Name and Surname");
         }
    }
}

您需要检查 Split(' ') 是否为您提供了 2 个名称。如果它只包含一个元素,它不能给你一个 numPrenume[1]。

未经测试的代码:

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text.Length != 0)
    {
        var numePrenume = textBox1.Text.Trim().Split(' ');

        if(numePrenume.Count()>1)
        {
            var nume = numePrenume[0];
            var prenume = numePrenume[1];

            var connString = @"Data Source=C:\Users\Andrei\Documents\Visual Studio 2010\Projects\Stellwag\Stellwag\Angajati.sdf";
            using (var conn = new SqlCeConnection(connString))
            {
            }
        }

        //some code
    }

}