I have multiple variables which I set prior to creating an object, I want to check if any of those variables in null, if any variable then display an error. Is there a way to incorporate this in a foreach loop?
For eg.
Var Var1 = blah1;
Var Var2 = blah2;
Var Var3 = blah3;
Var Var4 = blah4;
Var Var5 = blah5;
foreach(var above, if any is null)
Errmessage
Thanks in advance
I, personally, would have separate checks for each variable. On “error message” for multiple validation checks is a bad idea.
The main reason for this is that your “error message” should likely be an
ArgumentNullException, which should provide the proper parameter name. This will be different per variable. Even if you use a custom exception, providing information about which variable was improperly specified is worth the extra coding effort.That being said, if you want to do this, you can use: