Checking for equality to the same value for more than one variable C# -


is there shorthand version of comparison statement:

if (txtid.text != string.empty && txtname.text != string.empty && txtbod.text != string.empty && txtphone.text != string.empty) {//do } 

is there like: if these textboxs values != string.empty, something

you put textboxes in array , use linq that:

textbox[] boxes = new [] {txtid, txtname, txtbod, txtphone}; if (boxes.all(b => !string.isnullorempty(b.text)))     // 

you should store array in member variable of window don't have create again every check.


or (as habib pointed out) if textboxes in 1 container control and textboxes on control use this:

if (containingcontrol.controls.oftype<textbox>().all(b => !string.isnullorempty(b.text)))     // 

oftype<>() returns enumeration of controls in containingcontrol's control collection of type textbox , can iterate through sequence all.


note (as others pointed out) it's better practice use string.isnullorempty() comparing against string.empty (since null check included, though should not matter @ textbox).


Comments