Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

marked as duplicate by Michael K, Larry Coleman, Steve Evers, Job, ChrisF Jul 28 '11 at 21:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Am I the only one who finds it funny that a Civil Engineer's example is about an intersection? – Shawn D. Jul 28 '11 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 – dassouki Jul 28 '11 at 19:42
You mean like this: ? I think C# can do this with a nullable boolean. PL/SQL I has booleans that I think are nullable by default. – FrustratedWithFormsDesigner Jul 28 '11 at 19:47
lol, there once was a daily wtf with a custom boolean: true, false, filenotfound. – Falcon Jul 28 '11 at 19:58
+1 for TROLLEAN – Mchl Jul 28 '11 at 20:36

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
share|improve this answer
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. – dassouki Jul 28 '11 at 19:39
@dassouki You can implement something like an Enum in Python. Check… – Marcelo Jul 28 '11 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.

share|improve this answer
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 Jul 28 '11 at 20:27
@Michl - true - but I wasn't presenting it as a solution for his example. – ChrisF Jul 28 '11 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 Jul 28 '11 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;
share|improve this answer

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.

share|improve this answer

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.:


  • 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.

share|improve this answer

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.