Jump to content

How to see if a field has an Enum applied to it? (C#)

RileyTheFox

Hey. I'm current working on a project and all has been good up until now. I need to be able to check if a field has an Enum on it. How would I do this?

Here's some of the code that I'm working with right now.

 

public enum Flags : byte
	{
		NONE,
		CHORD,
		EXTENDEDSUSTAIN,
		STAR_POWER = 4,
		STAR_POWER_END = 8,
		SOLO_BEGIN = 16,
		SOLO_END = 32,
        	DOUBLED = 64
	}
note.flags |= Note.Flags.DOUBLED;

Now when the statement "flags |= Note.Flags.DOUBLED;" is called, how do I retrieve this one specific enum on the field "note.flags"

 

So lets say I have note.flags containing:

 

CHORD,

STAR_POWER,

DOUBLED

 

How would I be able to check if doubled is just there at all? 

 

I hope this made sense in the context I gave, thanks. 

CPU: Intel Core i7 8700  

GPU: Gigabyte GeForce GTX 1070

MOBO: ASUS Z370-F STRIX  

RAM: 16GB Corsair Vengeance DDR4 2133MHz

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, EvilCat70 said:

So lets say I have note.flags containing:

 

CHORD,

STAR_POWER,

DOUBLED

 

How would I be able to check if doubled is just there at all? 

 

I hope this made sense in the context I gave, thanks. 

Do an AND operation on Note.Flags with the enum you want. It'll either equal the enum or 0.

 

EDIT: If you want to check if multiple enums exist, OR the ones you want to check for, then AND it with Notes.Flags.

Link to comment
Share on other sites

Link to post
Share on other sites

using System;

namespace FlagsTest
{

    internal class Program
    {
        
        //Ensure [Flags] attribute to allow bitwise testing
        //Also use 1 bitshifting to ensure flags are powers of 2 (1, 2, 4, 8, etc)
        [Flags]
        public enum Flags : byte
        {
            NONE             = (1 << 0),
            CHORD            = (1 << 1),
            EXTENDEDSUSTAIN  = (1 << 2),
            STAR_POWER       = (1 << 3),
            STAR_POWER_END   = (1 << 4),
            SOLO_BEGIN       = (1 << 5),
            SOLO_END         = (1 << 6),
            DOUBLED          = (1 << 7)
        }
    
        //Older (.NET Framework pre-4.0)
        private static void testFlagsOldMethod(Flags flag) 
        {
            var hadChord           = (flag & Flags.CHORD) == Flags.CHORD;
            var hasExtendedSustain = (flag & Flags.EXTENDEDSUSTAIN) == Flags.EXTENDEDSUSTAIN;
            var hasStarPower       = (flag & Flags.STAR_POWER) == Flags.STAR_POWER;
            var hasStarPowerEnd    = (flag & Flags.STAR_POWER_END) == Flags.STAR_POWER_END;
            var hasSoloBegin       = (flag & Flags.SOLO_BEGIN) == Flags.SOLO_BEGIN;
            var hasSoloEnd         = (flag & Flags.SOLO_END) == Flags.SOLO_END;
            var hasDoubled         = (flag & Flags.DOUBLED) == Flags.DOUBLED;
            Console.WriteLine("Flag.CHORD? {0}", hadChord);
            Console.WriteLine("Flag.EXTENDEDSUSTAIN? {0}", hasExtendedSustain);
            Console.WriteLine("Flag.STAR_POWER? {0}", hasStarPower);
            Console.WriteLine("Flag.STAR_POWER_END? {0}", hasStarPowerEnd);
            Console.WriteLine("Flag.SOLO_BEGIN? {0}", hasSoloBegin);
            Console.WriteLine("Flag.SOLO_END? {0}", hasSoloEnd);
            Console.WriteLine("Flag.DOUBLED? {0}", hasDoubled);
        }
        
        //Newer (.NET Framework 4.0 and newer)
        private static void testFlagsNewMethod(Flags flag) 
        {
            var hadChord           = flag.HasFlag(Flags.CHORD);
            var hasExtendedSustain = flag.HasFlag(Flags.EXTENDEDSUSTAIN);
            var hasStarPower       = flag.HasFlag(Flags.STAR_POWER);
            var hasStarPowerEnd    = flag.HasFlag(Flags.STAR_POWER_END);
            var hasSoloBegin       = flag.HasFlag(Flags.SOLO_BEGIN);
            var hasSoloEnd         = flag.HasFlag(Flags.SOLO_END);
            var hasDoubled         = flag.HasFlag(Flags.DOUBLED);
            Console.WriteLine("Flag.CHORD? {0}", hadChord);
            Console.WriteLine("Flag.EXTENDEDSUSTAIN? {0}", hasExtendedSustain);
            Console.WriteLine("Flag.STAR_POWER? {0}", hasStarPower);
            Console.WriteLine("Flag.STAR_POWER_END? {0}", hasStarPowerEnd);
            Console.WriteLine("Flag.SOLO_BEGIN? {0}", hasSoloBegin);
            Console.WriteLine("Flag.SOLO_END? {0}", hasSoloEnd);
            Console.WriteLine("Flag.DOUBLED? {0}", hasDoubled);
        }

    
        public static void Main(string[] args) {
            var testFlags = Flags.NONE;
            testFlags |= Flags.DOUBLED;
            testFlags |= Flags.STAR_POWER;
            testFlagsOldMethod(testFlags);
            Console.WriteLine();
            testFlagsNewMethod(testFlags);
        }
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×