I need to know how I could handle my problem in a efficient and smart way:
I want an error-handling for user's input; I have 2 fields CustomerID
and Company
:
if (customerIDExists)
{
errorMessage = "...";
}
if (companyInvalid)
{
errorMessage = "...";
}
else
{
// Add user to DB
}
There could be 3 errors:
With the example above the else is for the last if (companyInvalid). How can I deal with the else that this only executes when the 1 and 2 if doesn't match?
Else if is not right because I have to check all opportunities.
Well, you could make it very explicit:
if (customerIDExists)
{
...
}
if (companyInvalid)
{
...
}
if (!customerIDExists && !companyInvalid)
{
// Add user to DB
}
Or just use the fact that you haven't got an error message:
string errorMessage = null;
if (customerIDExists)
{
errorMessage = ...;
}
if (companyInvalid)
{
errorMessage = ...; // Don't forget it could already be non-null!
}
if (errorMessage == null)
{
// No error message, so we must be fine
// Add user to DB
}
See more on this question at Stackoverflow