Return to Snippet

Revision: 17799
at May 20, 2010 09:53 by jimfred


Updated Code
// Given this enum...
    [FlagsAttribute] 
    enum MyStatus: ushort
    {
        Bit0 = 0x0000,
        Bit1 = 0x0001,
        Bit2 = 0x0002,
        Bit3 = 0x0004
    };


// Cast a 0x7 to MyStatus and render a string for testing/demonstration purposes:
((MyStatus)0x7).ToString() results in string "Bit0, Bit1, Bit2"

// To test a bit, use code of the form. Assumes status is a MyStatus.
if ( Convert.ToBoolean( status & MyStatus.Bit0) )

Revision: 17798
at May 20, 2010 09:49 by jimfred


Updated Code
// Given this enum...
    [FlagsAttribute] 
    enum MyStatus: ushort
    {
        Bit0 = 0x0000,
        Bit1 = 0x0001,
        Bit2 = 0x0002,
        Bit3 = 0x0004
    };


// Cast a 0x7 to MyStatus and render a string for testing/demonstration purposes:
((MyStatus)0x7).ToString() results in string "Bit0, Bit1, Bit2"

// To test a bit, use code of the form 
if ( Convert.ToBoolean( status & MyStatus.Bit0) )

Revision: 17797
at September 14, 2009 13:23 by jimfred


Initial Code
// Given this enum...
    [FlagsAttribute] 
    enum MyStatus: uint
    {
        Bit0 = 0,
        Bit1 = 1,
        Bit2 = 2,
        Bit3 = 4
    };


// Cast a 0x7 to MyStatus and render a string for testing/demonstration purposes:
((Ts2Dll.Ts2Dll.MyStatus)0x7).ToString() results in string "Bit0, Bit1, Bit2"

Initial URL
http://msdn.microsoft.com/en-us/library/system.flagsattribute%28VS.71%29.aspx

Initial Description
FlagsAttribute is useful to render a string indicating which status bits or flag bits are turned on. The resulting string will contain the name of the enum bit that is turned on without needed to maintain a separate string table.

Note the inheritance-like syntax that determines the size of the enum. This is useful to adjust the size of enum structure members for marshaling with C.

Initial Title
Render names of C# bitfields using enums and  FlagsAttribute. Also, adjusting size of enum using, like ushort, for marshalling p

Initial Tags


Initial Language
C#