Create an enum where the values correspond to bits in an integer. Adding the Flags attribute enabled you to do some more bit operations on the enum values.
[Flags]
public enum CanBe {
Sold = 1,
Bought = 2,
Exchanged = 4
}
Now you can just use the or operator between the values:
CanBe can = CabBe.Sold | CanBe.Exchanged.
You can add a state with the |= operator:
can |= CanBe.Sold;
Or several states:
can |= CanBe.Sold | CanBe.Bought;
You can keep a state with the &= operator:
can &= CanBe.Sold;
Or several states:
can &= CanBe.Sold | CanBe.Bought;
You can remove states by using the ~ operator to create a complement to a value:
can &= ~CabBe.Bough;
Or seveal states:
can &= ~(CabBe.Bough | CanBe.Exchanged);
You can check for a state using the & operator:
if ((can & CanBe.Sold) != 0) ...
Or several states at once:
if ((can & (CanBe.Sold | CanBe.Bought)) != 0) ...
Or check that several states are all set:
if ((can & (CanBe.Sold | CanBe.Bought)) == (CanBe.Sold | CanBe.Bought)) ...