Switch Statement (String) Selects Default Every time

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,

    If(messageNumber_txt.Contains("WARN003"),"WARN003",
    If(messageNumber_txt.Contains("INFO003"),"INFO003",
    If(messageNumber_txt.Contains("ERROR004"),"ERROR00",
    If(messageNumber_txt.Contains("INFO01"),"INFO01",
    If(messageNumber_txt.Contains("INFO007"),"INFO007",
    "Default")))))

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.

This looks perfect
May I know what is the value of messageNumber_txt

Try like this
messageNumber_txt.ToString.ToUpper.Contains(“WARN003”)
Like wise for all

Cheers @HsDev

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.

  If(errorWindow){
    message = The whole error message
    messageArray = message.split("  ") //two spaces
    messageNumber_txt = messageArray(0)
    message = messageArray(1)
   
         Switch(If(messageNumber_txt.Contains("WARN003"),...
 }

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 tried using .ToUpper like you suggested, and I’m still getting the default case…

1 Like

Then we need to check with the whole error message
Can I have a sample of it pls if possible
Cheers @HsDev

We can resolve this easily
We are almost
Cheers @HsDev

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.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.