# Checkbox Switch Statement

**URL:** <https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [May 20, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837 "2022-05-20T14:36:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [May 20, 2022, 2:36pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/1 "2022-05-20T14:36:27Z")

</div>

I have 4 checkboxes and a text field. If the user selects checkbox1, I want “1” to display in the text field. If the user selects checkbox2, I want “2” to display in the text field. And so on…

I have the following code written in the expression binding of the text field:

```auto
switch(
	({../checkbox1.props.selected}=true)=1,
	({../checkbox2.props.selected}=true)=2,
	({../checkbox3.props.selected}=true)=3,
	({../checkbox4.props.selected}=true)=4,
	1,2,3,4,
	"1",
	"2",
	"3",
	"4"
)

```

If I select checkbox1, I get “2” displayed in the text field and selecting the other checkboxes displays a “3” in the text field.

I am not sure what I am doing wrong here…

Thank you.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 20, 2022, 2:41pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/2 "2022-05-20T14:41:45Z")

</div>

Good news 🙂  
You’ve reinvented the `binEnum` expression function:  
[https://docs.inductiveautomation.com/display/DOC81/binEnum](https://docs.inductiveautomation.com/display/DOC81/binEnum)  
You should be able to just switch to passing your four checkboxes to binEnum.

---

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [May 20, 2022, 3:05pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/3 "2022-05-20T15:05:59Z")

</div>

Thank you! I got the first part working.

Here’s my next question…  
So what if a user selects checkbox1 and checkbox2, how can i get a value of say “5” displayed?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 20, 2022, 3:10pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/4 "2022-05-20T15:10:00Z")

</div>

Let’s back up a bit (this seems like it might be an XY problem, of sorts).

What is your end goal with these checkboxes; what do they represent (in abstract terms) and what does the desired output represent?

Is there a truth table you can create of checkbox states → numeric output? _Can_ this be boiled down a simple repeating pattern?

---

<div class="post-metadata">

**Author:** ![csilk](https://avatars.discourse-cdn.com/v4/letter/c/9e8a1a/32.png) [@csilk](https://forum.inductiveautomation.com/u/csilk)\
**Post date:** [May 20, 2022, 3:17pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/5 "2022-05-20T15:17:14Z")

</div>

So I am creating a “service report” and the 4 checkboxes represent different types of equipment. When a user is filling out the service report, they select the checkbox(s) based on the equipment they worked on. The text field that is displaying the selected values, won’t be in the final design. Instead, I will just be writing the value, of the selected checkbox(s), to a parameter and then passing the parameter to a table which displays the information entered in the service report. I was only using the text field for development purposes, to get the functionality working.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [May 20, 2022, 3:19pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/6 "2022-05-20T15:19:24Z")

</div>

Add a custom `value` property to each of the checkboxes. Name it whatever you want. `key` is the default and is fine. Set the value to the “weight” of that checkbox.

![Check sum](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/6/8615d9f4fcdd9c07215b40f2174bcbf81fe00a42.gif)

Create an expression binding on the component to track the total.

```auto
{../Checkbox.custom.key} * {../Checkbox.props.selected}
 + {../Checkbox_0.custom.key} * {../Checkbox_0.props.selected} 
 + {../Checkbox_1.custom.key} * {../Checkbox_1.props.selected} 
 + {../Checkbox_2.custom.key} * {../Checkbox_2.props.selected}

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 20, 2022, 3:19pm UTC](https://forum.inductiveautomation.com/t/checkbox-switch-statement/59837/7 "2022-05-20T15:19:38Z")

</div>

So you’re encoding the parameter state, then unpacking it at the other end.

You _could_ do that (unambiguously) using `binEnum`s sibling function, [`binEnc`](https://docs.inductiveautomation.com/display/DOC81/binEnc) - which packs a set of bits into binary, then returns the integer representation.

However, you really probably don’t need it. Parameters are free. I would just add parameters for each of these input parameters. It’s going to be a _lot_ easier to reason about down the road.
