4 Replies - 350 Views - Last Post: 20 September 2013 - 08:00 AM Rate Topic: -----

#1 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Shorten up to make it easier to read

Posted 19 September 2013 - 11:20 AM



             if (SequentialSearch(Boynames, txtBoy.Text) != -1)  
             {
                 lblBoy.Text = "Yes";
                 lblBoy.Visible = true;
             }
             else
             {
                 lblBoy.Text = "No";
                 lblBoy.Visible = true;
             }


              if (SequentialSearch(Girlnames, txtGirl.Text) != -1)
             {
                 lblGirl.Text = "Yes";
                 lblGirl.Visible = true;
             }
             else
             {
                 lblGirl.Text = "No";
                 lblGirl.Visible = true;
             }

              if (SequentialSearch(Boynames, txtBoy.Text) != -1 && SequentialSearch(Girlnames, txtGirl.Text) != -1)
              {
                  lblBoth.Text="Yes";
                  lblBoth.Visible = true;
              }
              else
              {
                  lblBoth.Text="No";
                  lblBoth.Visible = true;
              
              }




Just looking to shorten this up and have the same thing, if at all possible. I was gonna nest them, but when tried it failed.

Is This A Good Question/Topic? 0
  • +

Replies To: Shorten up to make it easier to read

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

Re: Shorten up to make it easier to read

Posted 19 September 2013 - 11:43 AM

I don't usually do this, but it's much easier to illustrate my points with an example rather than an explanation:

bool boyFound = SequentialSearch(Boynames, txtBoy.Text) != -1;
bool girlFound = SequentialSearch(Girlnames, txtGirl.Text) != -1;
UpdateLabels(boyFound, girlFound);

//...
private void UpdateLabels(bool boyFound, bool girlFound)
{
    UpdateLabel(boyFound, lblBoy);
    UpdateLabel(girlFound, lblGirl);
    UpdateLabel(boyFound && girlFound, lblBoth);
}

private void UpdateLabel(bool val, Label lbl)
{
    lbl.Visible = true;
    if(val)
        lbl.Text = "Yes";
    else
        lbl.Text = "No";
}


You should use methods so you only have to write code once.

Also, you could simplify the if statement even further if you use the inline-if operator:

lbl.Text = val ? "Yes" : No";

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5822
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: Shorten up to make it easier to read

Posted 19 September 2013 - 02:37 PM

Curiously, the code always sets visible true, so:
lblBoy.Visible = lblGirl.Visible = lblBoth.Visible = true;
int found = ((SequentialSearch(Boynames, txtBoy.Text) != -1)?1:0) + ((SequentialSearch(Girlnames, txtGirl.Text) != -1)?2:0);
lblBoy.Text = (found & 1 == 1) ? "Yes":"No";
lblGirl.Text = (found & 2 == 2) ? "Yes":"No";
lblBoth.Text = (found & 3 == 3) ? "Yes":"No";



Yes, I'm being a little silly, but Curtis beat me to it and I had to do something.
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

Re: Shorten up to make it easier to read

Posted 19 September 2013 - 02:41 PM

I'm guessing he has the labels hidden until he gives them text, so it makes sense to show them no matter what. I think, anyway.

Nice and brief; love reading clever code online. Though if I found that in my source control I'd probably punch my monitor.
Was This Post Helpful? 0
  • +
  • -

#5 Wizmeister  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 17-October 12

Re: Shorten up to make it easier to read

Posted 20 September 2013 - 08:00 AM

Thank you guys :) Yes, i had to set the labels to false on the form, instructor likes it that way, then change the text to display and make visible. Thank you for the help though! I appreciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1