How To Use Switch Statement to check if a string belongs to one List ?
Example :
String can be cell from one column
Liste 1 = {“A”,“B,“C”,“C”,“D”}
Liste 2 ={“Alpha”, “Beta”, Gama”, “Theta”}
Liste 3 ={“Item1”, “Item2”,“Item3”}
…
…
Liste 10 =
Note: I’m using ForEach Activity, and due to the fact that I have many lists ( In my project is 10), so it’s somewhat tedious , that’s why I’m thinking to use Switch Statmenent, and in case there is better solution it will be Good. Thx
The Switch activity does not really allow you to work that way, nevertheless, you can “trick” it with an if nested inside the condition, and make it work as you want!
What you want to do is the following expression, but a bit more complex:
if(Condition,ValueIfTrue,ValueIfFalse)
You can use this expression inside the Switch condition, and assign values like numbers to “ValueIfTrue” or “ValueIfFalse” to reach your goal:
1- Define Arrays
2- Switch with Int32 Type (in my example, you can do it in a different way if you want) with the following condition (example for 3 contains, can be expanded)
The result of this condition is a 1 if the inputString is inside List1, a 2 if the string is inside Liste2, and lastly a 3 if the string is inside the Liste3:
Note: The “99” at the end, is to have an option, in case our input string is not inside any Case.
Thanks a lot @ignasi.peiris , Probably I didn’t explain well what is the input and what should be the output.
Let me ask differently :
Keeping all above Assigned variables/Arrays, Suppose I have string " Alpha Bank" as input :
How to check that this string has SubString, which is “Alpha” belonging to var_Liste2 ? It’s Like spliting inputs string to substring and check if at least one of them belongs to one of the list
Note : In case this will become more complicated, than I will keep existing workflow using ForEach Activity.
You then can do the following (to follow a similar structure)
1- Get all substrings, like on the example, you can use " " as delimiter:
Split(var_InputString," ")
This should output: {“Alpha”,“Bank”}
Then you can use a For Each SubString (For Each - String Type) to loop them, and check with LinQ if they’re present in any of the previous arrays (I did as @ppr suggested, merge them into an Array of Arrays to make it easier)
Hi @postwick , In fact both @ignasi.peiris & @ppr , gives an alternative ideas for this to avoid using Else IF, still only some adjustment to achieve this. Let’s see if they can do that or I will Keep using Else IF Statement.
Thanks @ppr , from here I can only have the total items from different lists ( Same approach as @ignasi.peiris ), Is it possible to know in which List this PartialString belongs to ?
Then when found, just print the FoundVariable(0).ToString.
Another approach you can do is using Invoke Code (C#mode, couldn’t make it work in VB.Net, I believe it’s not supported yet) and use “NameOf” Expression:
You need somehow to make your way and change the “var_Liste” from the snippet above, and it will print the name, but I don’t think it’d be the best option based on what we have now.
If you can modify the Lists / Arrays, you can try and go with the 1st option, otherwise, you can try the 2nd one.