Switch Expression

Hi there. I have a transaction group I’m trying to set up, and in that transaction group I have a expression item. Purpose of this expression item is to read a tag value and assign a string return based on the numeric value.


This only returns null for a value, even when the tag is a value of 1. No idea why this doesn’t work. Any ideas?

Edit: that last line 'Unknown" was added, it didn’t work without it either. I thought maybe it needed something there to handle the ‘default’

I typically use the case() function. It’s the exact same but the syntax is more readable imo: case - Ignition User Manual 8.1 - Ignition Documentation


There isn’t any reason to have the extra unknown lines since that would be your default value upon no match.

case( {[Rim_Joist]BoardType},
	1, "1X3",
	2, "1X4",
	3, "1X6",
	4, "1X8",
	5, "2X4",
	6, "2X6",
	7, "2X8",
	8, "2X10",
	9, "2X12",
	10, "1X2",
	11, "2X2",
	12, "2X3",

I would agree that the case function is more readable, @stuart.williams but when I hit the little functions button I didn’t see case there, cause I’m blind apparently. :grinning:

I’ll try it like you put down @dkhayes117 , thanks. Would that extra crap be the reason it doesn’t work, or would it be the formatting? I would expect it should return something the way I wrote it, not null.

I agree with dkhayes. Just removing all the extra "unknown"s makes it more readable and also covers a lot of bases with just one line.

Also, if the code in your initial post was copied and pasted exactly, you’re missing the ending double quote on your final unknown. Another thing is you are missing the default case which is required for the switch statement. You have 20 cases with 20 returns, but no default case. That might have been why it was not compiling.

I don’t see the option to view why the expression is failing in the transaction group. My suggestion for troubleshooting in the future would be to create an expression tag and past the expression in it. If there’s a problem with the code, the “Diagnostics” tab on the expression tag will give you more information.


Pretty sure the reason it wasn’t working was simply that the switch function sucks so bad! :roll_eyes: I used it and hated it when I first started ignition as well, before I found the case statement. There’s really no place for it at all; its only purpose is to confuse and frustrate people with its unnecessary separation of test condition and result. Your example is the golden example that demonstrates this


Might have been useful if we’d been able to unpack arrays in expressions, so you could match an array of keys with an array of values ? I don’t know, I hate it too :X

There’s a lookup() function for that (for datasets).


I agree. The addition of case was much appreciated. switch is a great candidate for deprecation.

1 Like

If the data is used in more than one spot, it would make sense to put it in a dataset tag and use the lookup function.