C# Using Switch Statement to check if a string contains a word

With C# (Studio 2021.10.3), I want to use a switch statement to check a string for the words “SUCCESS” and “FAILED”

Non-invokable member ‘If’ connect be used like a method, so how can I do like below?

Same question but with VB:

HI @spatulaCity

Can you share the whole expression you passed in the switch?


Hi @Gokul001
If(StatusMessage.Contains(“SUCCESS”), “SUCCESS”, If(StatusMessage.Contains(“FAILED”), “FAILED”, “Default”))

I’m guessing C# doesn’t allow If String.Contains
But I’m not sure…

aKeys = new string {“SUCCESS”, “FAILED”}
sKeyResult = aKeys.FirstOrDefault(s= >StatusMessage.Contains(s))

Switch Expression sKeyResult

the if oneliner in VB we are doing with the IF Method
in C# we can do:

condition ? OKReturn : KOreturn;

Thank you, @ppr one-line is what I was looking for

For future reference:
StatusMessage.Contains(“SUCCESS”) ? “SUCCESS” : StatusMessage.Contains(“FAILED”) ? “FAILED” : “Default”

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