-1
 private bool[] if_checkbox_enabled()
 {
     bool[] rst = new bool[5];
     rst[0] = (bool)a.IsChecked;
     rst[1] = (bool)b.IsChecked;
     rst[2] = (bool)c.IsChecked;
     rst[3] = (bool)d.IsChecked;
     rst[4] = (bool)e.IsChecked;
     return rst;
 }

correct case:

bool[] result = { false, false, false, false, false };
if (if_checkbox_enabled() == result)//no problem
{...}

wrong case:

if (if_checkbox_enabled() == { false, false, false, false, false })//reports error
{...}

Why can't I directly use a bool type array for the judgement statement? Thanks!

Jeff
  • 27
  • 7
  • Your question doesn't make sense, because your "no problem" scenario is just as problematic as the "reports error" scenario. You can't compare arrays like that, because even in the "no problem" scenario, it won't work (you're just comparing references, which won't ever be equal). See marked duplicate for how you _can_ compare arrays. And keep in mind that it's questionable whether comparing arrays is really the best way for you to implement this logic in the first place. – Peter Duniho Jan 02 '18 at 06:21
  • @Rise: this question isn't about assignment, so your proposed edit to change the title isn't valid. – Peter Duniho Jan 02 '18 at 06:22
  • Thanks. I have 2^5=32 cases as each IsChecked has 2 states. Is comparing arrays the best way? @ Peter Duniho – Jeff Jan 02 '18 at 06:27
  • _"Is comparing arrays the best way?"_ -- unknown, as there's not enough detail to know. Do you actually have 32 different outcomes? I.e. 32 different actions you will execute, depending on the exact combination? If so, why? What are those actions? If not, then how does the smaller number of actions map to the combinations available from the checkboxes? – Peter Duniho Jan 02 '18 at 06:31
  • In your example, all you care about is the case where all five are `false`, which is easily handled by using the LINQ `All()` operator (e.g. `if (if_checkbox_enabled().All(f => !f))`). Even if there are more conditions, it might still be possible to generalize groups of conditions. – Peter Duniho Jan 02 '18 at 06:31
  • Thank you very much. I will keep what you told me in mind. @Peter Duniho – Jeff Jan 02 '18 at 06:34

1 Answers1

0

Your first example is wrong too. You can't compare the arrays like that. You can compare them in a simply loop by per element or you can use SequenceEqual;

bool[] result = { false, false, false, false, false };
if (if_checkbox_enabled().SequenceEqual(result))
{
      //Do something          
}

or

if (if_checkbox_enabled().SequenceEqual(new bool[]{ false, false, false, false, false }))
{

}
lucky
  • 12,734
  • 4
  • 24
  • 46