简体   繁体   中英

Array IndexOutOfRange

        string temp = textBox1.Text;
        char[] array1 = temp.ToCharArray();
        string temp2 = "" + array1[0];
        string temp3 = "" + array1[1];
        string temp4 = "" + array1[2];
        textBox2.Text = temp2;
        textBox3.Text = temp3;
        textBox4.Text = temp4;

How do I prevent an IndexOutOfRange Error occurring when a user inputs less than three letters in textBox1?

How i will prevent IndexOutOfRange Error if a user only input less than three letters in the textBox1?

Just check that using temp.Length :

if (temp.Length > 0)
{
    ...
}

... or use switch / case .

Also, you don't need the array at all. Just call ToString on each character, or use Substring :

string temp = textBox1.Text;
switch (temp.Length)
{
    case 0:
        textBox2.Text = "";
        textBox3.Text = "";
        textBox4.Text = "";
        break;
    case 1:
        // Via the indexer...
        textBox2.Text = temp[0].ToString();
        textBox3.Text = "";
        textBox4.Text = "";
        break;
    case 2:
        // Via Substring
        textBox2.Text = temp.Substring(0, 1);
        textBox3.Text = temp.Substring(1, 1);
        textBox4.Text = "";
        break;
    default:
        textBox2.Text = temp.Substring(0, 1);
        textBox3.Text = temp.Substring(1, 1);
        textBox4.Text = temp.Substring(2, 1);
        break;
}

Another option - even neater - is to use the conditional operator:

string temp = textBox1.Text;
textBox2.Text = temp.Length < 1 ? "" : temp.Substring(0, 1);
textBox3.Text = temp.Length < 2 ? "" : temp.Substring(1, 1);
textBox4.Text = temp.Length < 3 ? "" : temp.Substring(2, 1);

Another way to do it is using ElementAtOrDefault :

    string[] temp = textBox1.Text.Select(c => c.ToString());
    string temp2 = "" + temp.ElementAtOrDefault(0);
    string temp3 = "" + temp.ElementAtOrDefault(1);
    string temp4 = "" + temp.ElementAtOrDefault(2);
    textBox2.Text = temp2;
    textBox3.Text = temp3;
    textBox3.Text = temp4;

A general solution to this kind of problem is to check the length of the source value (array, string, or vector) before accessing its elements. For example:

string  temp = textBox1.Text;

if (temp.Length > 0)
    textBox2.Text = temp.Substring(0, 1);
if (temp.Length > 1)
    textBox3.Text = temp.Substring(1, 1);
if (temp.Length > 2)
    textBox4.Text = temp.Substring(2, 1);

if the size is fixed ie no. of character is 3 or length is 3 then it must be like .....

string temp = textBox1.Text; char[] array1 = temp.ToCharArray(); if(temp.length==3) { string temp2 = "" + array1[0]; string temp3 = "" + array1[1]; string temp4 = "" + array1[2]; textBox2.Text = temp2;
textBox3.Text = temp3; textBox3.Text = temp4; }
string temp = textBox1.Text; char[] array1 = temp.ToCharArray(); if(temp.length==3) { string temp2 = "" + array1[0]; string temp3 = "" + array1[1]; string temp4 = "" + array1[2]; textBox2.Text = temp2;
textBox3.Text = temp3; textBox3.Text = temp4; }
this will be work if ur string length is 3....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM