0
        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?

2
  • You should check out temp's length (temp.Length) and see it it's above three, otherwise show an error or do nothing. Commented Jul 18, 2013 at 16:18
  • Can we assume textBox3.Text = temp4; this "3" is a typo and not an error and should be "4"? Commented Jul 18, 2013 at 16:24

4 Answers 4

10

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@doctorlove: No, because I'm assuming that as many characters as possible should be filled - see my edits.
0

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);

Comments

0

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;

2 Comments

temp2 will be "\0" not string.Empty anymore
That will append a U+0000 when it's too short - it's not clear that that would be desirable.
0

if the size is fixed i.e 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; }
this will be work if u r string length is 3....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.