I'm doing some validation where I need to check for certain combinations between two values. For example, if string1 is "fruit", valid values for string2 are "apple", "banana" and "pear". Currently, I'm doing this:
switch(string1)
{
case "fruit":
if(string2 != "apple" && string2 != "banana")
{
return false;
}
break;
case "meat":
if(string2 != "beef" && string2 != "pork")
{
return false;
}
default:
return true;
break;
}
This is really two questions. The first is, is there any good way to do something more like this:
switch(string1)
{
case "fruit":
if(string2 NOT IN ("apple", "banana"))
{
return true;
}
break;
case "meat":
if(string2 NOT IN ("beef", "pork"))
{
return false;
}
default:
return true;
break;
}
The second part of this question is likely what will get answered first: is there a better/best way to do this? I'm not the most amazing coder in the world and this is the first "off the top of my head" solution, so I'm certainly open to better ones. Thanks!