# Why isn't there a Boolean for x values of a variable? [duplicate]

Possible Duplicate:
Would you see any use of a Trilean (True, False, ??)

Well first and foremost, I'm not a programmer, I am a civil engineer that does some programming and quite enjoy it.

Second and Second most, I'd like to claim rights to this amazing invention and call it TROLLEAN or DASSOUKIEAN.

Joking aside, on numerous times I run into variables that can only hold three values (let's cal them True, False, and Other.

For example, take a traffic intersection. It could be signalized, unsignalized, or non-controlled (aka no stop signs and no traffic lights). it would be dnice to define `TROLLEAN traffic_light = Other` Other in this case represents non-controlled intersections. Instead I find myself writing a code sequence to define that.

• Am I the only one who finds it funny that a Civil Engineer's example is about an intersection? Commented Jul 28, 2011 at 19:40
• If i was a computer scientist I would've given an example on star wars or trek. Unfortunately, I don't follow Fantasia Commented Jul 28, 2011 at 19:42
• @Michael - I didn't see that question, If the mods see fit, they can close my question Commented Jul 28, 2011 at 19:44
• You mean like this: en.wikipedia.org/wiki/Three-valued_logic ? I think C# can do this with a nullable boolean. PL/SQL I has booleans that I think are nullable by default. Commented Jul 28, 2011 at 19:47
• lol, there once was a daily wtf with a custom boolean: true, false, filenotfound. Commented Jul 28, 2011 at 19:58

I may be mis-understanding your question, but I would think an Enum would suffice here. In the case of your traffic light analogy:

``````Enum TrafficLights
{
Signalised,
un-signalised,
non-controller
}
``````
• Thanks, I didn't know about enum as I'm familiar with python. I've played with the C/C++ before, but never really got too deep. Commented Jul 28, 2011 at 19:39
• @dassouki You can implement something like an Enum in Python. Check stackoverflow.com/questions/36932/… Commented Jul 28, 2011 at 20:35

You would have to have a different type for each value, which while possible and true to the spirit of strong typing is probably overkill for most purposes.

It's far simpler to use a general enumeration or even integer value that can have as many different values as you require. This satisfies 99.9% of real world cases (which is what we should be concerned about here).

In fact languages do have three way values with the concept of a nullable boolean that can be `null` (for undefined), `true` or `false` - though obviously it doesn't work for the example you've given where an enum would be the best solution.

• Null however would not fit the 'intersection example' from question. Null usually means 'no information if this is true or false' - for example after a tornado sweeps through the intersection we don't know if traffic lights are still there and working. In the example we have just three different and distinct cases (and can not be really sure if there will never be more) - this fits enumerated datatype better.
– Mchl
Commented Jul 28, 2011 at 20:27
• @Michl - true - but I wasn't presenting it as a solution for his example.
– ChrisF
Commented Jul 28, 2011 at 20:30
• I know. I just wanted to elaborate about it a little, since people sometimes actually use null as 'third boolean value'.
– Mchl
Commented Jul 28, 2011 at 20:34

In C# you could use a nullable boolean if you wanted to avoid the enum. Something like:

``````bool? IsTrue = true;
bool? IsFalse = false;
bool? IsMaybe = null;
``````

This concept does exist. It is called an enum. An enum variable can be one of the n states listed in the declaration of the enum type.

A commonly used term is 'tri-state', and it is usually implemented in one of two ways:

• Using an enumeration specially crafted for the purpose, e.g.:

```enum Tristate { TRISTATE_TRUE, TRISTATE_FALSE, TRISTATE_UNDECIDED };```

• Using a nullable boolean, e.g. (in C#) `bool? tristate;`. A nullable boolean can hold either a regular boolean value (true or false), or `null`, which can be interpreted as 'no value' or 'undecided'.

Tristates are pretty common for GUI radio buttons: a radio button can be checked, unchecked, or grayed-out.

All of the answers so far pretty much have it.

As recently as C++, languages didn't even have a "boolean" type. If such a type was needed, either an enum or a typedef int was used to make bool LOOK like a type. It was only when the next generation of web and Windows-oriented languages came around, such as VB, Java and C#, that "bool" was deemed important enough to include as a "built-in" type.

What a "trilean" value's third value might be is very context-dependent. "True" and "False" are very general, and can be easily translated in the mind of the programmer to "yes" and "no" or simply "on" and "off". Programmers think this way; they have to, because that's the way the computer thinks. Under all the layers of abstraction are billions and billions of binary digits, which are represented by the continuity of microscopic circuits in the CPU and other hardware; on or off, no in-between.

Adding this third wheel confuses things. What's the third value? Null? Not Specified? Unknown? Other (and what else could there be in an answer space with "on" and "off")? This third value becomes VERY context-dependent, and for this reason "trileans" are not built-in.

Just like simple booleans before they were built-in, you can always create the types you need in virtually any strongly-typed language. A "trilean" would be created using an Enum with "true", "false" and whatever your third value needs to be. In .NET, you could either use a nullable bool, where null would be your third "value". Or, there are several enumerated "tri-state" types in the Framework libraries; DataGridViewTriState is a big one, and its values are simply True, False and NotSet which would work in a large number of cases.