0

I am programming in C# i need to put some things in a string 'xml'

I have the following code

        TextBox[] myTextBoxes = new TextBox[] { this.textBox2, this.textBox3, this.textBox4, this.textBox5, this.textBox6, this.textBox7, this.textBox8, this.textBox9, this.textBox10, this.textBox11 };
        TextBox[] ValueBoxes = new TextBox[] { this.textBox3, this.textBox5, this.textBox7, this.textBox9, this.textBox11 };
        CheckBox[] myCheckBoxes = new CheckBox[] { this.checkBox2, this.checkBox4, this.checkBox6, this.checkBox8, this.checkBox10 };
        CheckBox[] myMandBoxes = new CheckBox[] { this.checkBox3, this.checkBox5, this.checkBox7, this.checkBox9, this.checkBox11 };

and to verify certain condition i have

 xml += "<fields>";

        for (int i = 0; i < myTextBoxes.Length; i++)
        {
            if (string.IsNullOrWhiteSpace(myTextBoxes[i].Text))
            {
                if (myCheckBoxes[i].Checked == true)
                    xml += "<field display='yes'> ";
                else
                    xml += "<field display='no'> ";
                if (myMandBoxes[i].Checked == true)
                    xml += "<mandatory='yes'>";
                else
                    xml += "<Mandatory='no'>";
                xml += "<label>" + (string)myTextBoxes[i].Text + "</label>";

            }
        }

It gives me an Indexoutof boud exception at if (myCheckBoxes[i].Checked == true)

How can i resolve this

Guillaume
  • 1,782
  • 1
  • 25
  • 42
  • you have way more textboxes in `myTextBoxes` than checkboxes in `myCheckBoxes` – jeroenh Nov 27 '12 at 10:09
  • 1
    You said you are 'programming in c++', but you add snippet and tag as [C#]. – Steve B Nov 27 '12 at 10:10
  • You design seems weird to me. Can you explain your requirement? I suspect a proper use of DataBinding will be far more readable, and maintenable. And why not using XmlWriter or any Xml class in the .NEt framework? This will be far more robust – Steve B Nov 27 '12 at 10:12

7 Answers7

2

Your arrays have different amounts of elements in them so you cannot access them all by using the same index. They will have to be done independently.

Ric
  • 12,855
  • 3
  • 30
  • 36
  • can you please cite as to how – user1838130 Nov 30 '12 at 12:05
  • myMandBoxes and myCheckBoxes both have the same number of items in them, 5 each, but myTextBoxes has many more. in your for loop, you are using the number of items from myTextBoxes and since it has more, once the number of iterations has exceed 5, there are no more items in myCheckBoxes and therefore you get the out of bounds exception. Is that ok? – Ric Nov 30 '12 at 16:58
0

You don't have same number of checkboxes as Textboxes so you can't access them against the same index. Your index is starting from 0 to myTextBoxes.Length, if i is going to be greater than 4, then you will get this exception.

Not really sure what you are trying to do but you can add a check against the length before accessing the element from myCheckBoxes.

if (i < myCheckBoxes.Length && myCheckBoxes[i].Checked == true)
        xml += "<field display='yes'> ";
Habib
  • 219,104
  • 29
  • 407
  • 436
0

Use Foreach instead

Here edit as myTextBoxes.Length - 1

for (int i = 0; i < myTextBoxes.Length - 1; i++) 

EDIT:

Also your checkBox count and Textox count does not tally

sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
0

Your for loop is running to myTextBoxes.length, but you are iterating through myCheckBoxes. It seems you have more textboxes than CheckBoxes

Change it to:

for (int i = 0; i < myCheckBoxes.Length; i++) {

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
0

Instead of

for (int i = 0; i < myTextBoxes.Length; i++)

use

for (int i = 0; i < myCheckBoxes.Length; i++) // or myMandBoxes.Length (Both the CheckBoxes have same items)

Because you are looping with CheckBoxes so why having a length of TextBox.

Mr_Green
  • 40,727
  • 45
  • 159
  • 271
0

Your text box has 11 elements, while checkbox has 5 elements.

Change following

 if (string.IsNullOrWhiteSpace(myTextBoxes[i].Text))

to

 if (string.IsNullOrWhiteSpace(myTextBoxes[i].Text) && i < myCheckBoxes.Length )
Tilak
  • 30,108
  • 19
  • 83
  • 131
0

First of all, never use += on strings when constructing them with loops, they are immutable, and using it this way, it's just a waste of speed and memory. Use a StringBuilder instance instead.

As for your question, the length of the myTextBoxes array is 10, so you have items at indexes 0 through 9. The code in this line

if (myCheckBoxes[i].Checked == true)

tries to access the corresponding item in the myCheckBoxes array that has 5 elements (indexed 0 to 4). When i becomes 5, the code accesses the myCheckBoxes[5] that does not exist, hence the exception.

Other than that, textBox2 or checkBox7 are simply awful names for controls, and there are better options in the .net Framework for XML serialization and generation.

Community
  • 1
  • 1
SWeko
  • 30,434
  • 10
  • 71
  • 106
  • StringBuilder benefits appears only when the number of concatenated string reach a certain threshold (don't remember which). When it's very low, simple concatenation is acceptable (if it's assumed!) – Steve B Nov 27 '12 at 10:14
  • 1
    regarding string vs string builder, http://www.codeproject.com/Articles/6771/String-Vs-StringBuilder-C. StringBuilder is not good to use in all cases. – Tilak Nov 27 '12 at 10:14
  • Sorry, I meant to specify that += should not be used in this case (the question very much qualifies for StringBuilder usage :) Will edit. – SWeko Nov 27 '12 at 10:19