My Switch Statement takes a string, to see if it either equals or contains an error. It’s not selecting the correct case. I’m not sure why. It always selects the default case. If anyone can help me figure this out I would greatly appreciate,
As of right now. This is what I have, and it’s actually all on one line, with no spaces. Before I tried it this way, I used the equals method, because I would actually like it to be 100% that it equals the same thing, but I could get away with using the Contains method. Either way. I just can’t get this to select anything else besides the default value.
The messageNumber_txt is just the error number. It’s what I’m using to identify what needs to happen in the switch statement. Because each error or info message needs to be handled differently. It’s just a string that is something like “INFO001” or “ERROR007”. This is how it’s set up in my code.
They are all handled differently, and the default case is supposed to only execute if the message number hasn’t already been handled, so that I can keep adding to my switch statement in the future to handle new errors.
I don’t know why my switch statement was only selecting the default option but, I changed the switch statement to only take messageNumber_txt as the expression… instead of using the if statements, and Contains or Equals method, I think the switch statement uses an equals method anyway, and I would only have to use the .Contains in a switch, If I was comparing a larger set of text that may be variable. My text is not variable, so I only included the messageNumber_txt in the expression field and nothing else. I want the cases in the switch statement to only execute if the strings are equal. I did a test, using “name” = Bob or name=“bob” and it selected the correct one, for every case I had.
So I could have done…
name = "Bob"
Switch( name )
{
Case: default{....}
Case 1 Bob {....}
Case 2 bob {....}
Case 3 BOB {....}
}
Or It could have also been like this.
name = "Why do I always pick the name Bob?"
Switch(
if(name.Contains("Bob"), "Bob,
if(name.Contains("bob"), "bob",
if(name.Contains("BOB"), "BOB",
"default"
)));
{
Case default{....}
Case 1 Bob{...}
Case 2 bob{....}
case 3 BOB{....}
}
So I guess it depends on the application. But this is how I resolved it.