Bitwise Definitions with Enums
The default type for an enum is an int (32-bit integer) which gives you 32 bit positions to work with. It's the same thing as if you had written
public enum Types : int { ... }
Depending on how many bit positions you need, you have access to
- byte (8-bit unsigned)
- sbyte (8-bit signed)
- short (16-bit signed)
- ushort (16-bit unsigned)
- int (32-bit signed)
- uint (32-bit unsigned)
- long (64-bit signed)
- ulong (64-bit unsigned)
where signed numbers are represented in 2's complement. So, I'm not sure what bit position Basic is supposed to represent, but if you set the value to -1 as in your example the equivalent 32-bit value is 0xFFFFFFFF (all one's). Trying to use each bit position as a flag would mean that you have turned all of your flags "on".
There's also a slight mathematical mistake in your assignment of the flag values in that you've put in the decimal values to correctly activate only a bit at a time, but you've listed the values as hexadecimal which will not give you the effect that you are looking for.
Hexadecimal listings are nice for those who work at the bit level often because each hex number represents 4-bits. The following is the corrected version of your enum with hex values:
public enum Types
{
A = 0x0001,
B = 0x0002,
C = 0x0004,
D = 0x0008,
E = 0x0010,
F = 0x0020,
G = 0x0040
}
Or, if you wanted to write the equivalent using decimal values:
public enum Types
{
A = 1,
B = 2,
C = 4,
D = 8,
E = 16,
F = 32,
G = 64
}
The important point for using the Flags attribute, is that each value in the enumeration must only activate a single bit position. A useful trick on declaring binary values here which I've re-iterated below using your enum. This clearly identifies what you are trying to capture using the Flags attribute with the bit-shift operator to identify your enum values.
public enum Types
{
A = 1,
B = 1 << 1,
C = 1 << 2,
D = 1 << 3,
E = 1 << 4,
F = 1 << 5,
G = 1 << 6
}
Displaying Enum Values in a Listbox
Finally, on to what you're trying to display. I'm just going to assume that you're trying to do this with WPF. If you're using some other means of generating your UI, you'll have to search further.
I borrowed and tested this from this short article: Bind Enum to a WPF ListBox using ObjectDataProvider. You're basically using the ObjectDataProvider to bind to the enum, then binding the result to the listbox. I've assumed that you've defined your enum in the namespace WpfTest for this example.
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:Types" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<StackPanel>
<ListBox DataContext="{Binding Source={StaticResource EnumValues}}" ItemsSource="{Binding}"/>
</StackPanel>
</Window>
Doing this, your listbox will always show all of the enumerations that you define in Types.