Using Enum to store multiple bool fields

By | January 24, 2017

Hello, Once I attended an interview for the C# programming job. And programmer asked me a question. Though I was not able to answer the question. But I will share here the question and the answer using enum in this post which can help you to encounter any such scenario in your job.

Question

Suppose I have a Class named HotelRoom as shown in the code below.

These fields are getting stored in the database in corresponding table columns. Now I want to add some bool properties to the HotelRoom class. The properties could be like IsTVPresent, IsSingleOccupancy, IsDoubleOccupancy etc. So my new class looks as shown in the code below

And I have to add the corresponding new columns to my database column. But is it the correct design? Should I keep changing my class and DB table design every time I add a new boolean property to the class.

Solution using Enum

Enumeration comes to the rescue to handle such a scenario. I will discuss the steps one by one.

Enums can be used to hold the multiple states and best way to do it is using different values of integer. We will create an enum which will contain all the bool values for the Hotel Room and assign each of the the integer values of 2 to the power of n.

The above enum contains all the values which should have been present in the HotelRoom class as Boolean properties. The new look of the class will look like as following.

So instead of many properties I have only single enum property in the class which will take care of the boolean properties.

Lets see how to achieve it.

Here I have created a HotelRoom instance which has both AC and TV. I have used a or bit operator to get the result. I will discuss in the next section how it works.

Now this object I can store in the DB or any data store with only single field for all the booleans.

Now the retrieving part. Once I have retrieved the values from the DB I need to know which are all the boolean values present for an instance.

In the above code I am getting the room instance from some data source. And to check if a particular boolean value is present in the enum we need to use the “AND” bitwise operator with the result and the enum value which we want to check.

How all this magic is happening?

Since I am assigning some values which are power of 2 to the enums. Now these values are stored a binary in the system based on the processor (32 bit or 64 bit). For simplicity I will show only 4 bit.

OR bitwise operator

OR bitwise operator

 

In the above figure you can see, we are using OR operation for the “AC” and “TV” bit values. And we will get the value 0110 which is 6 in decimal. This will be stored in the database.

Now comes the retrieval part.

Masking to retrieve the result

Masking to retrieve the result

 

To get if a particular value is present or not. We will use the masking. We will use the AND bitwise operator with the “Result” and the “Mask” operands. The mask is the enum value whose presence we need to find in the result.

And if the result is non zero, we can infer that the particular value is present.

Is this the perfect Method for storing bool values?

If there are only booleans which do not depend on one another for setting or resetting then in that case it is a very perfect way to store the booleans. But of we have booleans which depend on each other then we should not use this way to store the values.

Conclusion:

In this article I have discussed a way to store many boolean values in a single field and how we can prevent to keep adding fields for each and every boolean field.

 

 

  • Dmitry Bytchenko

    Please, mark RoomFacility enum with [Flags] attribute