Skip to main content
added Flags attribute, showed a more readable way to define combination flags, showed use of easier to ready HasFlag method.
Source Link

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

[Flags] public enum ExampleEnum { One = 0b0001, Two = 0b0010, Three = 0b0100 }; 

The Flags attribute tells the compiler that you're setting up values which don't overlap. The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & .HasFlag(ExampleEnum.One)) { CallOne(); } if (myEnum & .HasFlag(ExampleEnum.Two)) { CallTwo(); } if (myEnum & .HasFlag(ExampleEnum.Three)) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly and marked with the Flags attribute. But you can do that by changing the values of the enums in your example to:

[Flags] public enum ExampleEnum { One = 0b0001, OneAndTwoTwo = 0b00110b0010, OneAndThreeThree = 0b01010b0100, TwoOneAndTwo = 0b0010One | Two, TwoAndThreeOneAndThree = 0b0110One | Three, ThreeTwoAndThree = 0b0100Two | Three }; 

When you write a number in the form 0bxxxx, you're specifying it in binary. So you can see that we set bit 1, 2, or 3 (well, technically 0, 1, or 2, but you get the idea). You can also name combinations by using a bitwise OR if the combinations might be frequently set together.

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

public enum ExampleEnum { One = 0b0001, Two = 0b0010, Three = 0b0100 }; 

The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & One) { CallOne(); } if (myEnum & Two) { CallTwo(); } if (myEnum & Three) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly. But you can do that by changing the values of the enums in your example to:

public enum ExampleEnum { One = 0b0001, OneAndTwo = 0b0011, OneAndThree = 0b0101, Two = 0b0010, TwoAndThree = 0b0110, Three = 0b0100 }; 

When you write a number in the form 0bxxxx, you're specifying it in binary. So you can see that we set bit 1, 2, or 3 (well, technically 0, 1, or 2, but you get the idea).

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

[Flags] public enum ExampleEnum { One = 0b0001, Two = 0b0010, Three = 0b0100 }; 

The Flags attribute tells the compiler that you're setting up values which don't overlap. The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum.HasFlag(ExampleEnum.One)) { CallOne(); } if (myEnum.HasFlag(ExampleEnum.Two)) { CallTwo(); } if (myEnum.HasFlag(ExampleEnum.Three)) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly and marked with the Flags attribute. But you can do that by changing the values of the enums in your example to:

[Flags] public enum ExampleEnum { One = 0b0001, Two = 0b0010, Three = 0b0100, OneAndTwo = One | Two, OneAndThree = One | Three, TwoAndThree = Two | Three }; 

When you write a number in the form 0bxxxx, you're specifying it in binary. So you can see that we set bit 1, 2, or 3 (well, technically 0, 1, or 2, but you get the idea). You can also name combinations by using a bitwise OR if the combinations might be frequently set together.

Use binary constants
Source Link
user1118321
  • 5k
  • 1
  • 20
  • 25

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

public enum ExampleEnum { One = 0x10b0001, Two = 0x20b0010, Three = 0x40b0100 }; 

The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & One) { CallOne(); } if (myEnum & Two) { CallTwo(); } if (myEnum & Three) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly. But you can do that by changing the values of the enums in your example to:

public enum ExampleEnum { One = 0x10b0001, OneAndTwo = 0x30b0011, OneAndThree = 0x50b0101, Two = 0x20b0010, TwoAndThree = 0x60b0110, Three = 0x40b0100 }; 

When you write a number in the form 0bxxxx, you're specifying it in binary. So you can see that we set bit 1, 2, or 3 (well, technically 0, 1, or 2, but you get the idea).

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

public enum ExampleEnum { One = 0x1, Two = 0x2, Three = 0x4 }; 

The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & One) { CallOne(); } if (myEnum & Two) { CallTwo(); } if (myEnum & Three) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly. But you can do that by changing the values of the enums in your example to:

public enum ExampleEnum { One = 0x1, OneAndTwo = 0x3, OneAndThree = 0x5, Two = 0x2, TwoAndThree = 0x6, Three = 0x4 }; 

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

public enum ExampleEnum { One = 0b0001, Two = 0b0010, Three = 0b0100 }; 

The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & One) { CallOne(); } if (myEnum & Two) { CallTwo(); } if (myEnum & Three) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly. But you can do that by changing the values of the enums in your example to:

public enum ExampleEnum { One = 0b0001, OneAndTwo = 0b0011, OneAndThree = 0b0101, Two = 0b0010, TwoAndThree = 0b0110, Three = 0b0100 }; 

When you write a number in the form 0bxxxx, you're specifying it in binary. So you can see that we set bit 1, 2, or 3 (well, technically 0, 1, or 2, but you get the idea).

Source Link
user1118321
  • 5k
  • 1
  • 20
  • 25

I find the code hard to read with the goto statements. I would recommend structuring your enum differently. For example, if your enum was a bitfield where each bit represented one of the choices, it could look like this:

public enum ExampleEnum { One = 0x1, Two = 0x2, Three = 0x4 }; 

The code that calls this code could set the appropriate bit. You could then do something like this to make it clear what's happening:

if (myEnum & One) { CallOne(); } if (myEnum & Two) { CallTwo(); } if (myEnum & Three) { CallThree(); } 

This requires the code that sets up myEnum to set the bitfields properly. But you can do that by changing the values of the enums in your example to:

public enum ExampleEnum { One = 0x1, OneAndTwo = 0x3, OneAndThree = 0x5, Two = 0x2, TwoAndThree = 0x6, Three = 0x4 };