Questions tagged [enum-flags]

167 questions
5
votes
1 answer

Parsing multiple enum values (Flagged): Reading in a filter type from a query string

I'm plan to make a page that displays info about user's in the form of a table. Each column will be a property of the table. I want someone viewing the table to be able to choose ways to filter the users. There will be two check boxes: 1 for…
Teeknow
  • 1,015
  • 2
  • 17
  • 39
4
votes
3 answers

C#: Getting the correct keys pressed from KeyEventArgs' KeyData

I am trapping a KeyDown event and I need to be able to check whether the current keys pressed down are : Ctrl + Shift + M ? I know I need to use the e.KeyData from the KeyEventArgs, the Keys enum and something with Enum Flags and bits but I'm not…
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
4
votes
7 answers

Flags enum in .NET

I am trying to use a set of conditional statements that will set up an enumeration attributed with [Flags]. However, the compiler complains that 'm' is unassigned. How can I rewrite the following to achieve my intended functionality? Media m; if…
deckerdev
  • 2,766
  • 2
  • 21
  • 23
4
votes
2 answers

Converting string to flags enum in C#

A device reports status of its limit switches as a series of ones a zeros (meaning a string containing "010111110000"). Ideal representation of these switches would be a flags enum like this: [Flags] public enum SwitchStatus { xMin, xMax, …
Lukas
  • 2,232
  • 3
  • 21
  • 34
4
votes
2 answers

Select all flagged values in enum using LINQ

I have a collection of flagged enums, like this: [Flags] enum EnumThing { A = 1, B = 2, C = 4, D = 8 } I'd like to select all flags in the collection using LINQ. Let's say that the collection is this: EnumThing ab = EnumThing.A |…
Br2
  • 167
  • 1
  • 10
4
votes
6 answers

Is there a way to tell if an enum has exactly one, multiple or no flags set?

I have an enum defined like that: [Flags] public enum Orientation { North = 1, North_East = 2, East = 4, South_East = 8, South = 16, South_West = 32, West = 64, North_West = 128 } Is there a generic way to tell if…
Norman
  • 3,279
  • 3
  • 25
  • 42
4
votes
2 answers

Best practice to determine the amount of flags contained in a Enum-flag combination?

In some scenarios, when I pass a Enum to a method, I need to handle whether it is a single Enum value, or otherwise it is a flag combination, for that purpose I wrote this simple extension: Vb.Net: Public Function FlagCount(ByVal sender…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
4
votes
2 answers

How should I represent a value of None when a [Flags] enum type doesn't define it?

While working with XNA today, I noticed that one of the enum types that uses [Flags] did not have a None = 0 value: [Flags] public enum Buttons { DPadUp = 1, DPadDown = 2, DPadLeft = 4, DPadRight = 8, Start = 16, Back = 32, …
Kyle Baran
  • 1,793
  • 2
  • 15
  • 30
4
votes
7 answers

Determine if an Enum value is NOT a composite in C#

EDIT: Most people suggest that flag enums should always have values of powers of two. That may be best practice but I am not defining enums here, rather checking them and want to cover all possible scenarios within reason. The question is really…
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168
4
votes
1 answer

Why do many WPF-classes store boolean values as enum-flags?

If you reflect over the WPF System.Windows.Controls.Control class you can see: public class Control : FrameworkElement { internal ControlBoolFlags _controlBoolField; // [...] internal bool ReadControlFlag(ControlBoolFlags reqFlag); …
HerpDerpington
  • 3,751
  • 4
  • 27
  • 43
4
votes
2 answers

How should I represent hierarchical flag enums?

I have the following set of enums: [Flags] public enum Categories : uint { A = (1 << 0), B = (1 << 1), B1 = B | (1 << 16), B2 = B | (1 << 17), B3 = B | (1 << 18), B4 = B | (1 << 19), B5 = B | (1 << 20), C = (1 << 2), …
dlras2
  • 8,416
  • 7
  • 51
  • 90
4
votes
4 answers

Parsing enum flags from comma-separated list or integer

I have an XML that contains several flags, some of them are unsigned 32-bit integers and others are unsigned 64-bit integers. Some of them are written in a comma-separated list and others are in hex style. See this…
richi3f
  • 47
  • 1
  • 6
4
votes
4 answers

Is it ok to add an "All" item to a Flags enum?

If I have an enum with multiple values which can be present at the same time, I create a Flags enum: [Flags] public enum Foo { None = 0, A = 1, B = 2, C = 4, D = 8 } If I now want to pass the fact that every value is set I would…
magnattic
  • 12,638
  • 13
  • 62
  • 115
3
votes
0 answers

Best way to unset a flag in C when using Enum that is signed

I'm trying to use enum in C to create a flags variable, as discussed here: Flags, enum (C) How should I reset a single enum flag, when the compiler represents the enum as signed? Problem: Say I declare my flags type like this: typedef enum { …
Luminaire
  • 334
  • 6
  • 11
3
votes
1 answer

How to store a large flags enum to a single column in a SQL database?

I have this enum : [Flags] public enum Actions { None = 0, MoveUp = 1, MoveDown = 2, MoveRight = 3, MoveLeft = 4 } and I would like have the columns of a table like this: Id | UserId | Actions .. | .. | .. I'm confused as…
resstel
  • 39
  • 6
1 2
3
11 12