.NET Tips and Tricks

Blog archive

Use Enumerated Values with Bit Flags to Handle Multiple Options

If your code uses a set of options and you don't use enumerated values, you can end up writing opaque code like this which passes an unexplained "magic number" to a method:

cust.Update(1)

With enumerated values, your code becomes easier to read because you can pass a named value:

cust.Update(UpdateOptions.Deferred)

Some applications need a set of options that can be combined in a variety of ways. In that scenario, a favorite developers' trick is to have each bit in a Byte represent one of the options. By using values that are powers of 2 and logical Ors, a developer can set individual bits in the Byte; using those same values against the Byte, along with logical Ands, a developer can check which bits in a Byte are set. Bringing in enumerated values makes working with bit flags easier to both read and write.

First, you need to override the default integer values for enumerated values to set the enumerated values to powers of 2, as in this enumeration:

<Flags>
Public Enum UpdateOptions
   Deferred = 1
   Audited = 2
   Transacted = 4
End Enum

To use these enumerated values with the Update method to specify both the Deferred and Audited options, you would Or together the enumerated values. The Visual Basic code looks like this (in C#, you would use the | operator instead of the Or):

cust.Update(UpdateOptions.Deferred Or  UpdateOptions.Audited)

Inside the Update method, to accept the Byte created by Oring together the options and to see if a particular option has been set, you use this code in Visual Basic (in C#, use the & operator instead of And):

Private Sub Update(Options As Byte)
If Options  And UpdateOptions.Deferred Then

You don't need the Flags attribute I put on my enumeration, but it does simplify debugging. The Flags attribute causes the ToString method to generate a useful result when used on a Byte that's been cast to the type of the enumeration. This example casts the Options variable to my UpdateOptions type, and then calls the result's ToString method:

Private Sub Update(Options As Byte)
Debug.WriteLine(CType(Options, UpdateOptions).ToString())

Because I used the Flags attribute on the UpdateOptions enumeration, the output is the informative "Deferred, Audited"; without the Flags attribute, the output would have been the less helpful "3".

Posted by Peter Vogel on 05/07/2013


comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube