How to optimize this code?

Of course, there should be many ways to optimize the following code, where I basically have to make sure that many text fields are not empty, and then read their values:

if (foo1.Text.Length != 0 & bar1.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo1.Text + " / " + bar1.Text;
}

if (foo2.Text.Length != 0 & bar2.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo2.Text + " / " + bar2.Text;
}

if (foo3.Text.Length != 0 & bar3.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo3.Text + " / " + bar3.Text;
}

if (foo4.Text.Length != 0 & bar4.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo4.Text + " / " + bar4.Text;
}

if (foo5.Text.Length != 0 & bar5.Text.Length != 0) 
{
    output.Text += myStrings[i] + " / " + foo5.Text + " / " + bar5.Text;
}

if (foo6.Text.Length != 0 & bar6.Text.Length != 0)
    output.Text += myStrings[i] + " / " + foo6.Text + " / " + bar6.Text;

if (foo7.Text.Length != 0 & bar7.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo7.Text + " / " + bar7.Text;
}

if (foo8.Text.Length != 0 & bar8.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo8.Text + " / " + bar8.Text;
}

if (foo9.Text.Length != 0 & bar9.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo9.Text + " / " + bar9.Text;
}

if (foo10.Text.Length != 0 & bar10.Text.Length != 0)
{
    output.Text += myStrings[i] + " / " + foo10.Text + " / " + bar10.Text;
}
+3
source share
7 answers

I would put repeating elements in arrays and then move them.

TextBox[] foos = new TextBox[] { foo1, foo2, foo3, /* etc */ };
TextBox[] bars = new TextBox[] { bar1, bar2, bar3, /* etc */ };

for (int i = 0; i <= 10; i++)
    if (foos[i].Text.Length != 0 && bars[i].Text.Length != 0)
        output.Text += myStrings[i] + "/" + foos[i].Text + bars[i].Text;

Of course, if the elements are really named sequentially, you can populate the arrays by looking at the controls from the collection of Form controls named "foo" + number.ToString ().

+9
source

I would just focus on the Controls collection that these TextBoxes are on, then it only filters the TextBox, and you check and concat.

StringBuilder + =.

+5

. .

  • , foo bar ,
  • foos . .
  • FooBar, foo bar . , FooBars, , .
  • 3 , . #, Map/Reduce LINQ (.Select(). Aggregate()), FooBars , .

, . , , .:)

( , .)

EDIT:

, , " ". 10 - , , ( i) .

, , , ? - . , ListView. , " " .

+2

, foo bar. foo1 bar1 foo10 bar10 , . foo bar, .

+1
 foreach (Control ctrl in Page.Controls) {
      if (ctrl is TextBox) {
          if (ctrl.Text.Length != 0) {
              output.Text += myStrings[i] + "/" + ctrl.Text;
           }
      }
 }

Unconfirmed, but should work. You could call anything with these text boxes.

+1
source

Could you make foo1-foo10 into an array, foo [10] is the same for bar? This will allow you to express this as a simple loop.

0
source

Is it possible to make a WebControl with text fields called foo and bar, and has a function such as:

if (foo.Text.Length != 0 & bar.Text.Length != 0)
    return myStrings[i] + " / " + foo.Text + " / " + bar.Text;
else
    return string.Empty;

Put ten of them on your page, then use:

output.Text = myControl1.FooBar + myControl2.FooBar + myControl3.FooBar + ...

(Still a little dirty, but not quite repetitive.)

0
source

Source: https://habr.com/ru/post/1703110/


All Articles