Nuno Gomes /* ASP.NET Adventures */

var myInterests = new { language : "C#", technology: "ASP.NET" };

Enum types, FlagsAttribute & Zero value – Part 2

In my previous post I wrote about why you should pay attention when using enum value Zero.

After reading that post you are probably thinking like Benjamin Roux: Why don’t you start the enum values at 0x1?

Well I could, but doing that I lose the ability to have Sync and Async mutually exclusive by design. Take a look at the following enum types:

[Flags]
public enum OperationMode1
{
    Async = 0x1,
    Sync = 0x2,
    Parent = 0x4
}

[Flags]
public enum OperationMode2
{
    Async = 0x0,
    Sync = 0x1,
    Parent = 0x2
}

To achieve mutually exclusion between Sync and Async values using OperationMode1 you would have to operate both values:

protected void CheckMainOperarionMode(OperationMode1 mode)
{
    switch (mode)
    {
        case (OperationMode1.Async | OperationMode1.Sync | OperationMode1.Parent):
        case (OperationMode1.Async | OperationMode1.Sync):
            throw new InvalidOperationException("Cannot be Sync and Async simultaneous");
            break;
        case (OperationMode1.Async | OperationMode1.Parent):
        case (OperationMode1.Async):
            break;
        case (OperationMode1.Sync | OperationMode1.Parent):
        case (OperationMode1.Sync):
            break;
        default:
            throw new InvalidOperationException("No default mode specified");
    }
}

but this is a by design constraint in OperationMode2. Why? Simply because 0x0 is the neutral element for the bitwise OR operation.

Knowing this singularity, replacing and simplifying the previous method, you get:

protected void CheckMainOperarionMode(OperationMode2 mode)
{
    switch (mode)
    {
        case (OperationMode2.Sync | OperationMode2.Parent):
        case (OperationMode2.Sync):
            break;
        case (OperationMode2.Parent):
        default:
            break;
    }

This means that:

  • if both Sync and Async values are specified Sync value always win (Zero is the neutral element for bitwise OR operation)
  • if no Sync value specified, the Async method is used.

Here is the final method implementation:

protected void CheckMainOperarionMode(OperationMode2 mode)
{
    if (mode & OperationMode2.Sync == OperationMode2.Sync)
    {
    } else { 
    }
}

All content above prove that Async value (0x0) is useless from the arithmetic perspective, but, without it we lose readability.

The following IF statements are logically equals but the first is definitely more readable:

if (OperationMode2.Async | OperationMode2.Parent)
{
}

if (OperationMode2.Parent)
{ 
}

Here’s another example where you can see the benefits of 0x0 value, the default value can be used explicitly.

    <my:Control runat="server" Mode="Async,Parent">

    <my:Control runat="server" Mode="Parent">

Posted: Jul 06 2012, 03:04 AM by nmgomes | with 6 comment(s)
Filed under: ,

Comments

WebDevelopment said:

Great web development, design and marketing company. Best <a style="outline: none; text-decoration: none;" href="http://ecommercepros.org">Long Island e commerce and successful web site</a> you can find online.

# August 19, 2012 12:07 AM

WebDevelopment said:

Great Long Island bestwebsite company company. Best <a style="outline: none; text-decoration: none;" href="http://websitedevelopmentny.org">Connecticut Internet Marketing</a> you can find.

# August 19, 2012 12:26 AM

icons said:

 I am am excited too with this question.

P.S. Please review <a href="ikonga.deviantart.com/.../Android-Menu-Icons-287648017">Android Menu Icons from Ikonga</a>

# September 22, 2012 6:36 AM

icon download said:

 The charming answer

<a href="www.hpixel.com/.../a>

# September 23, 2012 1:55 AM

icon collection said:

 What magnificent words

<a href="www.hpixel.com/.../a>

# September 24, 2012 8:39 AM

icons pack said:

<a href="www.ucjc.edu/blogs Excuse, not in that section.....</a>

# October 8, 2012 9:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)