i have a condition where the value should not be null or it should be either R or U.
i had tried with below method but still i am not able to succeed with the result.
if (Place == "" || Place != "R" || Place != "U")
{
response.Status = "FAILURE";
response.Message = "Place Criteria is required / Invalid (place should be either R(rural)/U(urban)";
}
eventhough i pass R or U it shows the error message . please help me with the solution.
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I suggest else if constructon; here we have 2 valid options (rural and urban) and all the others are incorrect ones:
if (Place == "R")
RuralRoutine(); //TODO: put the right code here
else if (Place == "U")
UrbanRoutine(); //TODO: put the right code here
else { // All the other options are invalid
response.Status = "FAILURE";
response.Message =
"Place Criteria is required / Invalid (place should be either R(rural)/U(urban)";
}
Please, note, that if you want to add, say, "I" for an “Industrial Area”, all you’ll have to do is to add one more else if:
...
else if (Place == "I")
IndustrialAreaRoutine();
else {
response.Status = "FAILURE";
...
Method 2
if(Place ==""||( Place!="R" && Place !="U"))
{
}
you want the Place not be null and be “R” or “U” so you should use && to make sure the 3(null,”R”,”U”) do not happen at the same time.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0