tags:

views:

460

answers:

3

im trying to create a loop to avoid copy pasting these lines 30 times.

the names is sum1 to sum30

br1txt1 to br30txt1

br1txt2 to br30txt2

// decimal sum30 = decimal.Parse(br30txt1.Text) + decimal.Parse(br30txt2.Text);

// sumTxt30.Text = sum30.ToString();

but the error im geting is that the textbox array seems to try to put the value of the textbox not the text box refrenc it self in to the array, how shold i fix this?

private void sumX()
    {
        TextBox[] sumTextboxNames;

        sumTextboxNames = new TextBox[29];

        for (int i = 1; i < 31; i++)
        {
            if (sumTextboxNames[0] == null)
            {
                int y = 0;
                foreach (Control c in this.Controls)
                {
                    if (c is TextBox && c.Name.StartsWith("sum"))
                    {
                        sumTextboxNames[y] = (TextBox)c;
                        y++;
                    }
                }
            }
            else
            {

            }
            string1 = "br" + i + "txt" + 1 + ".Text";
            string2 = "br" + i + "txt" + 2 + ".Text";
            string3 = "sumTxt" + i + ".Text";
            sum = decimal.Parse(string1) + decimal.Parse(string2);
            int x = i - 1;
            sumTextboxNames[x].Text = sum.ToString();  
        }  
   }

solution to this was this code.

private void sumX()
{  
    string string1;
    string string2;
    string string3;

    for (int i = 1; i < 31; i++)
    {
        string1 = "br" + i + "txt" + '1';
        string2 = "br" + i + "txt" + '2';
        string3 = "sumTxt" + i;

        TextBox s = (TextBox)this.Controls[string3];
        TextBox b1 = (TextBox)this.Controls[string1];
        TextBox b2 = (TextBox)this.Controls[string2];

        decimal sum = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
        s.Text = sum.ToString();
    }
+4  A: 

The following lines won't work at all:

string1 = "br" + i + "txt" + 1 + ".Text";
string2 = "br" + i + "txt" + 2 + ".Text";

As 1 and 2 are not strings and can not be concatenated to a string. That should give a compiler error right away.

In the following line, you're trying to add up the names of the text boxes as numbers - won't work, the names contain non-number characters.

sum = decimal.Parse(string1) + decimal.Parse(string2);

Anyway, you don't need to use the TextBox array at all. What you could do is:

for (int i = 1; i <= 30; i++)
{

    TextBox s = (TextBox)this.Controls[<Name for the S-Textbox>];
    TextBox b1 = (TextBox)this.Controls[<Name for the first sum textbox>];
    TextBox b2 = (TextBox)this.Controls[<Name for the second sum textbox>];

    s.Text = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
}

EDIT
Sorry, quoted wrong line from OP's source code.

EDIT 2
Forgot to cast to TextBox - this is required of course... Thanks for pointing it out, everybody.

Thorsten Dittmar
the 1 and 2 shold be easy to fix, "string1 = "br" + i + "txt" + '1'; that shol make the string i need, but i still dont get how i can use this in the for loop you typed
Darkmage
<Name for the S-Textbox> = string3 in your code, and so on, DarkMage
Matt Ellen
oki diden get it to work whna tryed the but when i did a cast (textbox) its working just nice. so like TextBox s = (TextBox)this.Controls[string3]; i will put the code i used in new awnser for refrence
Darkmage
Good point, you do need a cast to TextBox in there.
Matt Ellen
Edited my answer to include the missing casts :-)
Thorsten Dittmar
+1  A: 

Thorsten Dittmar's answers is the way you should go.

However, with respect to this code:

foreach (Control c in this.Controls)
{
    if (c is TextBox && c.Name.StartsWith("sum"))
    {
        sumTextboxNames[y] = (TextBox)c;
        y++;
    }
}

You should try a solution that uses LINQ.

For example

TextBox [] sumTextBoxes = (from t in this.Controls.Cast<Control>
                          where t is TextBox
                          && t.Name.StartsWith("sum")
                          select t).Cast<TextBox>().ToArray();
Matt Ellen
yes, LINQ looks very interesting, i will hav a look at this in the future.
Darkmage
+1  A: 

thanks to Thorsten this is what i ended up with

    string string1;
    string string2;
    string string3;

    private void sumX()
    {
        for (int i = 1; i < 31; i++)
        {
            string1 = "br" + i + "txt" + '1';
            string2 = "br" + i + "txt" + '2';
            string3 = "sumTxt" + i;

            TextBox s = (TextBox)this.Controls[string3];
            TextBox b1 = (TextBox)this.Controls[string1];
            TextBox b2 = (TextBox)this.Controls[string2];

            decimal sum = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
            s.Text = sum.ToString();
        }
Darkmage
Glad I could help. Just two more things: a) don't post a separate answer next time - just edit your question or post a comment (SO does not work like a forum, so the order of the answers will not always stay the same, which may cause confusion). b) why declare string1, string2 and string3 globally? Make them local to your SumX method.
Thorsten Dittmar
good point, will fix this.
Darkmage