We help IT Professionals succeed at work.
Get Started

How to get C# enums working for a  binary comparion.

whorsfall
whorsfall asked
on
755 Views
Last Modified: 2013-12-16
Hi,

I am having problems getting the following code to work. I am doing something dumb but can't see it. Anyway I want to be able to do a bit test with an enum but can't get it to comple.
Please let me know how to adjust the code below to make it work.

If it is not obvious I am trying to do a bit wise AND to see if certain flags have been set.

Thanks,

Ward
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EnumTest
{
    [Flags]
    public enum Account_Status : int
    {
        ADS_UF_SCRIPT = 1,         // 0x1
        ADS_UF_ACCOUNTDISABLE = 2,         // 0x2
        ADS_UF_HOMEDIR_REQUIRED = 8,         // 0x8
        ADS_UF_LOCKOUT = 16,        // 0x10
        ADS_UF_PASSWD_NOTREQD = 32,        // 0x20
        ADS_UF_PASSWD_CANT_CHANGE = 64,        // 0x40
        ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128,       // 0x80
        ADS_UF_TEMP_DUPLICATE_ACCOUNT = 256,       // 0x100
        ADS_UF_NORMAL_ACCOUNT = 512,       // 0x200
        ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048,      // 0x800
        ADS_UF_WORKSTATION_TRUST_ACCOUNT = 4096,      // 0x1000
        ADS_UF_SERVER_TRUST_ACCOUNT = 8192,      // 0x2000
        ADS_UF_DONT_EXPIRE_PASSWD = 65536,     // 0x10000
        ADS_UF_MNS_LOGON_ACCOUNT = 131072,    // 0x20000
        ADS_UF_SMARTCARD_REQUIRED = 262144,    // 0x40000
        ADS_UF_TRUSTED_FOR_DELEGATION = 524288,    // 0x80000
        ADS_UF_NOT_DELEGATED = 1048576,   // 0x100000
        ADS_UF_USE_DES_KEY_ONLY = 2097152,   // 0x200000
        ADS_UF_DONT_REQUIRE_PREAUTH = 4194304,   // 0x400000
        ADS_UF_PASSWORD_EXPIRED = 8388608,   // 0x800000
        ADS_UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION = 16777216,   // 0x1000000
        ADS_UNALLOCATED = 0
    };

    class Program
    {
        static void Main(string[] args)
        {
            int test = 18;

            if (test & Account_Status.ADS_UF_ACCOUNTDISABLE)
            {
                Console.WriteLine("Account Disabled");
            }

            if (test & Account_Status.ADS_UF_LOCKOUT)
            {
                Console.WriteLine("Account Locked Out");
            }

        }
    }
}
// Here are the errors I get:
//
// Error	1	Operator '&' cannot be applied to operands of type 'int' and 'EnumTest.Account_Status'
// Error	2	Operator '&' cannot be applied to operands of type 'int' and 'EnumTest.Account_Status'

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE