Learn how to use the switch function in expression bindings to evaluate conditional statements.

Video recorded using: Ignition 7.7


(open in window)

[00:00] It is very common to want to translate a number to string that we can display to a user. So for example, I have this tag here, 'writableinteger1'. If the value is zero, I want to display 'off' on this label. If the value is one, I want do display 'running'. If it's two, I want to show 'manual', and if it's three, I want to show 'faulted'. So I can use the expression language to make this happen. I combine the text property of the label to an expression. In the expression language, we can use a couple of the logic functions that we have. One is an 'if' statement which allows us to check to see if the value is equal to zero, and if it is, we return some value. But the only problem here is if I have so many different states that I have to nest 'if' statements together. It's better to use a 'switch' statement here, which can switch on a value and for any number of cases we can return a value. So basically, the first thing I want to do is bring in 'switch', then the first argument is that I want to switch on, which is going to be my tag here. So I'm going to bring in the tag using the tag button on the right hand side, bring in 'writableinteger1'. Now that I have that, I want to put all the cases. If it's zero, one, two and three. So I'm going to put commas between each of those. After that then I want to put the values for each of the cases, so the first value will be for zero, which in my case I'm going to show 'off'. The second value is going to be for one, which is 'running'. The third is going to be for two, which in my case is 'manual'. And lastly, it's going to be 'faulted' for three. Now just in case we want to provide a fallback, in case the tag is none of those states. So I can actually put in 'unknown' here for the case where the integer1 is something other than zero, one, two, or three. I'm going to end parenthesis here so I can finish out the switch statement, and so with a simple expression, I can translate that number to text on the screen. So as I press 'OK', I can see it right now that I used two so it's showing 'manual'. If I go and set this to three, it's going to show 'faulted', if I go back to one it's going to show 'running', and if I go something that it doesn't understand like maybe a six, then it's going to show 'unknown' here.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.