Simple Boolean Toggle

Simple Boolean Toggle

Keep up to date with CoreSolutions

Simple Boolean Toggle

Simple Boolean Toggle Title Image

Toggle Switch

I’m going to keep today’s blog post simple and to-the-point.

It’s pretty common in programming to need to toggle between 2 states (boolean). FileMaker Pro however, doesn’t provide a boolean field, nor does it provide a function that will toggle a number field from a 0 to a 1 or vice-versa. Because of this, I’m guessing that most FileMaker developers end up using “If” statements to accomplish this kind of thing. While using an “If” statement in a situation like this isn’t going to be all that expensive (clock-cycle wise), I’d like to encourage developers to think a little outside of the box. There’s almost always more than one way to accomplish what it is you’re trying to do, and toggling boolean values like we’re doing here is certainly no exception.

Here’s one way that this toggle can be handled:

Abs ( $$booleanValue - 1 )

That’s it!
Subtract one from the current value, then take the absolute value of your result.
If your value was a 0, then we’ll subtract one from it… giving us -1. Then we take the absolute value of that, and voila… we’re back to 1.
If your value was 1, we subtract one from that, and we’re down to zero. Taking the absolute value of 0 won’t change anything here.

 

Comments

Leave a Comment