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

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

LVL 1
whorsfallAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OrcbighterCommented:
If you are trying to do bit calculations use byte rather than int as the data type
see my example below:

byte a = 7;
byte b = 9;
int ORValue = a | b;
int ANDValue = a & b;
Console.Writeline(string.Format("{0} | {1} -> OR value is {2}", a, b, ORValue ));
Console.Writeline(string.Format("{0} & {1} -> AND value is {2}", a, b, ANDValue ));

output:
a | b -> OR value is 15
a & b -> AND value is 1
0
AshokCommented:
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 = 16;

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

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


HTH
Ashok
0
shadow77Commented:
This will compile without errors:
    class Program
    {
        static void Main(string[] args)
        {
            Account_Status test = (Account_Status) 18;

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

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

Open in new window

0
_Katka_Commented:
Ashok and shadow77, I think it should be. Just a quick shot.

regards,
Kate
class Program
    {
        static void Main(string[] args)
        {
            Account_Status test = (Account_Status) 18;

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

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

Open in new window

0
shadow77Commented:
Katka, the compiler doesn't like the use of & with an int and an Account_Status.  This will still generate compiler errors.

You might try something like
    if (((Account_Status) test & Account_Status.ADS_UF_ACCOUNTDISABLE) != 0)
if you want to keep test declared as an int.

These two lines are equivalent
    if ((test & Account_Status.ADS_UF_ACCOUNTDISABLE) != 0)
    if ((test & Account_Status.ADS_UF_ACCOUNTDISABLE) == Account_Status.ADS_UF_ACCOUNTDISABLE)

Account_Status.ADS_UF_ACCOUNTDISABLE is not zero and it's the only value possible other than 0; writing the code that way won't make the compiler any happier.  Yhe objection is to the use of operands of different types where the second is not convertible to the first.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.