Reputation: 42175
I have to map a Flags Enumeration to multiple combo boxes.
For example, the first 2 bits need to correspond to a combo box for screen contrast setting:
Bit 0 - 1: Contrast (0=Low / 1 = Medium / 2=high)
Bits 2 & 3 need to correspond to the speech volume
Bit 2 - 3: Speech volume (0=Low / 1 = Medium / 2 = High)
and bits 4 & 5 correspond to the Buzzer volume.
Bit 4 – 5: Buzzer volume (0=Low / 1 = Medium / 2 = High)
Bit 6 corresponds to Entry or Exit (i.e. if it's on it's Entry, if it's off it's exit)
Bit 6: Entry/exit indication
My Flags enum is defined as:
[Flags]
public enum RKP
{
Contrast0 = 1, // bit 0
Contrast1 = 2, // bit 1
SpeechVolume2 = 4, // bit 2
SpeechVolume3 = 8, // bit 3
BuzzerVolume4 = 16, // bit 4
BuzzerVolume5 = 32, // bit 5
EntryExitIndication = 64, // bit 6
}
What's the best way to map these to the appropriate combo boxes, and then convert the values of each combo box to the correct enumeration value to save it?
Upvotes: 0
Views: 297
Reputation: 5746
With your solution it is possible to create conflicting values, for example combining MediumSpeechVolume
and HighSpeechVolume
as Dan Puzey pointed out.
Does your solution have to be a flagged enum
? This could be solved using a simple class with the 4 required enums inside as properties. If you need the exact bit pattern generated by your current flags enum, create another property to expose, with custom get
and set
to translate the current values of your 4 main properties to the required bit pattern and back.
Upvotes: 2
Reputation: 56536
[Flags]
public enum RKP
{
LowContrast = 0,
MediumContrast = 1, // bit 0
HighContrast = 2, // bit 1
LowSpeechVolume = 0,
MediumSpeechVolume = 4, // bit 2
HighSpeechVolume = 8, // bit 3
LowBuzzerVolume = 0,
MediumBuzzerVolume = 16, // bit 4
HighBuzzerVolume = 32, // bit 5
ExitIndication = 0,
EntryIndication = 64, // bit 6
}
contrastComboBox.ItemsSource = new[] { RKP.LowContrast, RKP.MediumContrast, RKP.HighContrast };
contrastComboBox.SelectedItem = currentValue & (RKP.MediumContrast | RKP.HighContrast);
//and so on for each combo box...
//and when you want the result:
RKP combinedFlag = (RKP)contrastComboBox.SelectedItem | //other combo box items
You'll probably want to do something about what strings will be shown, but that's the basic idea.
Upvotes: 0