Reputation: 57
so i have this code part:
private int checkErrors() {
int error = 0;
if (!(nether.Text.Equals("true"))) { error += 1; }
if (!(nether.Text.Equals("false"))) { error += 1; }
if (!(fly.Text.Equals("true"))) { error += 1; }
if (!(fly.Text.Equals("false"))) { error += 1; }
if (!(achivments.Text.Equals("true"))) { error += 1; }
if (!(achivments.Text.Equals("false"))) { error += 1; }
if (!(whitelist.Text.Equals("true"))) { error += 1; }
if (!(whitelist.Text.Equals("false"))) { error += 1; }
if (!(pvp.Text.Equals("true"))) { error += 1; }
if (!(pvp.Text.Equals("false"))) { error += 1; }
if (!(commandBlock.Text.Equals("true"))) { error += 1; }
if (!(commandBlock.Text.Equals("false"))) { error += 1; }
if (!(spawnMonster.Text.Equals("true"))) { error += 1; }
if (!(spawnMonster.Text.Equals("false")) { error += 1; }
return error;
}
but in any way it will get me that 'error = 7'
because when one statement is true the other is false
so my question is:
Is there a way to compare 2 strings to a third string ?
other example: i have the string userInput
and i want, if userInput
not equals "a"
or "b"
to execute error += 1;
!
Upvotes: 0
Views: 83
Reputation: 91
Another approach you can use is to create a function that accepts 2 parameters. One parameter will be the userInput and the other parameter will be an array of values the userInput shouldn't be. It will return 1 or 0.
Example:
public int isBadValue(string userInput, string[] goodValues){
if(Array.IndexOf(userInput) > -1){
return 0;
}
return 1;
}
And in your code you can just do something like this:
string[] goodValues = {"a", "b"};
error += isBadValue("s", goodValues);
You can easily add more good values and the function will be able to handle it. Not sure if good values change based on input field that's why I didn't include it in the isBadValue function.
Upvotes: 0
Reputation: 5034
Just with two clauses :
if (!nether.Text.Equals("true") && !nether.Text.Equals("false")) { error += 1; }
Upvotes: 0
Reputation: 1500815
I suspect you're trying to find cases where it isn't "true" and it isn't "false". So something like:
if (spawnMonster.Text != "true" && spawnMonster.Text != "false")
{
error++;
}
Alternatively, express the condition once, and apply it to all your strings:
var strings = new[] { nether.Text, fly.Text, achivments.Text, whitelist.Text
pvp.Text, commandBlock.Text, spawnMonster.Text };
return strings.Count(t => t != "true" && t != "false");
Upvotes: 2