Here is what i am trying to do.
I have two bit fields in my database:
Archived(bit)
Deleted(bit)
What i would like to do is calculate a int depending on their vaules.
As an example, this would be in my model:
class Person {
Int32 Status {get;set;}
}
If Archived = true set the status to 1,
If Deleted = true set the status to 2,
If both are false, set the status to 0.
I want to do this with out using If statements, maybe there is some sort of boolean arithmetic that i can use?
Why don’t use an enum?
You can directly cast an enum value to integer.
And you can do the contrary, if you have an int.
Also if you don’t provide the possibility that Deleted and Archived can coexists together this is actually a possibility with booleans.
The number of possible values encoded by n boolean values is 2^n, so, since you have 2 booleans, you have 4 possible values, 00, 01, 10 and 11.
The problem is in the problem itself: it is wrong to encode that information with booleans. It should be an enum with only 3 possible values also in the DB.