FlagsAttribute, what I'm I doing wrong!

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

I'm trying to use the FlagsAttribute enum to be able to
provide multiple mutally exclusive flags, my enum is
described as follows:

[Flags]
public enum NoteTypes
{
NoNotes = 0x0,
ProgressionDecision = 0x1,
TextNote = 0x2,
AttachedFile = 0x4,
BulkRegrowRequest = 0x8,
Identified = 0x16,
}

I invoke it by passing in the note types required as a
Bitmask OR then using a bitmask AND to compare which flags
have been selected, as follows:

SomeMethod(NoteTypes.Identified);


SomeMethod(NoteTypes noteType)
{

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.TextNote))
{
MessageBox("Text Note added");
}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.AttachedFile))
{
MessageBox("Attached file added");
}

if (Convert.ToBoolean(m_noteTypes & NoteTypes.Identified))
{
MessageBox("Identified added");
}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.ProgressionDecision))
{
MessageBox("progress decision added");

}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.BulkRegrowRequest))
{
MessageBox("bulk grow added");
}
}

When I try to pass in a note type of 'identified' the bit
mask AND thinks that the bit is in all types, but if I
pass in 'ProgressionDecision' it works correctly i.e only
the progression decision is added.

I'm a doing something wrong in the bitwise compare or the
setting up of the flags enum?

Any help much appreciated.

John
 
Hi,

Identified = 0x16 should be Identified = 0x10

The bit pattern for 0x16 = 00010110 = 22
The bit pattern for 0x10 = 00010000 = 16

Hope this helps
 
doh!, I forgot about the hex I was working it out using
decimal, no more new year eve drinking for me!

cheers

John
-----Original Message-----
Hi,

Identified = 0x16 should be Identified = 0x10

The bit pattern for 0x16 = 00010110 = 22
The bit pattern for 0x10 = 00010000 = 16

Hope this helps

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza
Hi,

I'm trying to use the FlagsAttribute enum to be able to
provide multiple mutally exclusive flags, my enum is
described as follows:

[Flags]
public enum NoteTypes
{
NoNotes = 0x0,
ProgressionDecision = 0x1,
TextNote = 0x2,
AttachedFile = 0x4,
BulkRegrowRequest = 0x8,
Identified = 0x16,
}

I invoke it by passing in the note types required as a
Bitmask OR then using a bitmask AND to compare which flags
have been selected, as follows:

SomeMethod(NoteTypes.Identified);


SomeMethod(NoteTypes noteType)
{

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.TextNote))
{
MessageBox("Text Note added");
}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.AttachedFile))
{
MessageBox("Attached file added");
}

if (Convert.ToBoolean(m_noteTypes & NoteTypes.Identified))
{
MessageBox("Identified added");
}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.ProgressionDecision))
{
MessageBox("progress decision added");

}

if (Convert.ToBoolean(m_noteTypes &
NoteTypes.BulkRegrowRequest))
{
MessageBox("bulk grow added");
}
}

When I try to pass in a note type of 'identified' the bit
mask AND thinks that the bit is in all types, but if I
pass in 'ProgressionDecision' it works correctly i.e only
the progression decision is added.

I'm a doing something wrong in the bitwise compare or the
setting up of the flags enum?

Any help much appreciated.

John


.
 
Back
Top