bigbro_1985 wrote:
But I think you just answered another problem that I had, or gave me an idea rather. I actually wanted a bitwise enumeration to assign Booleans, right now I made a bitwise structure that converts the 8 bit Boolean value to byte. But I don't like what I did, it doesn't look clean to me. This is what I like about embedded development because you get all your bitwise operators.
Oh, this is another problem I solved a while ago. The solution is really comprehensive. Please see my first article from my short "enumeration" series:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^].
This article solves a set of different related problems at once, first of all, the problem is using
foreach
for enumerations (it's a shame that it is not done in .NET by default), and a pretty tricky problems if members with identical underlying integer values.
And one of the problems is the "bitwise" operations. My solution is two-fold: first I explain how to work "manually", in the section "2 Background: Life before Enumeration Class", subsection: "2.4 Iterating Bit Sets". And later on, I explain the advances brought by my generic class "Enumeration". So, you will have a choice and may decide to use the simple technique, if it's enough for you.
[EDIT]
It's possible that you cannot figure out how to read/assign bits as Boolean. Let's do it: you read my article, and here I'll explain the rest, about Boolean. The idea is to use
indexed property:
public enum OptionSet {
None = 0,
AlignX = 1 << 0,
AlignY = 1 << 1,
Transparent = 1 << 2,
Border = 1 << 3,
}
public class SomeContainerWithEnumerationProperty {
OptionSet optionSet;
public bool this[OptionSet index] {
get {
}
set {
}
}
}
SomeContainerWithEnumerationProperty myOptions =
new SomeContainerWithEnumerationProperty();
myOptions[OptionSet.Border] = true;
myOptions[OptionSet.AlignX | OptionSet.AlignX] = true;
myOptions[OptionSet.AlignX] = false;
I hope you got it. Some a bit tricky problem is: in this syntax, you can have only one such property. Can you have more than one? In principle, if property type is always
bool
, but the types if
index
different (different
enum
types or other different types), the compiler can resolve which one to use, but how to write it?
The idea is: put these properties in one or more separate interfaces and make your class implement all these interfaces, with explicit interface implementation syntax. So, one property is "this" of the class itself, as shown above, plus one or more coming from the implementation of interfaces.
—SA