Barrett Kuethen
Barrett Kuethen

Reputation: 1914

How do I easily check if all bools are true in asp.net c#?

I have a list of bools on every page. I'm writing some validation code and am checking to make sure each bool is true before the user moves on. What is the best way to check to see if all the bools are true or false?

I know I can hard code each one in, but I'm wondering if there is a better way.

Here are the list of bools:

public bool posTitleBool;
public bool firstNameBool;
public bool lastNameBool;
public bool titleBool;
public bool emailBool;
public bool phoneBool;
public bool passwordBool;
public bool passwordValBool;
public bool companyNameBool;
public bool address1Bool;
public bool address2Bool;
public bool cityBool;
public bool stateBool;
public bool zipBool;
public bool industryBool;

Any help is appreciated.

Thanks!

Upvotes: 0

Views: 342

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500595

Options:

  • Put them all into a collection and use LINQ (e.g. conditions.All(x => x))
  • Simply hardcode

    postTitleBool && firstNameBool && ...
    
  • Use reflection to fetch and check each field (are these really public fields, btw? Ick)

You might want to consider using some sort of mapping in the implementation so that you've just got a collection of values you can manipulate easily, but then give a property facade which gets or sets the right mapped value. Best of both worlds.

Upvotes: 5

Related Questions