Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

Flag enums with multiple zero values...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nathan Baulch - 24 Apr 2007 15:28 GMT
I'm having issues working with some legacy enums that have multiple zero
values.
Whenever I try to call ToString() on one of the non-zero values, all but the
first zero value is included.
Is there any way to get the non-zero value name without resorting to string
manipulation or manually iterating through all values?

[Flags]
enum TestEnum
{
   Zero = 0,
   AlsoZero = 0,
   NonZero = 1
}

static void Main()
{
   //all of these output "AlsoZero, NonZero"
   Console.WriteLine(TestEnum.NonZero);
   Console.WriteLine(Convert.ToString(TestEnum.NonZero));
   Console.WriteLine(TypeDescriptor.GetConverter(typeof(TestEnum)).ConvertToString(TestEnum.NonZero));
}

Cheers,
Nathan
Patrice - 24 Apr 2007 18:02 GMT
Any reason for using the flags attribute ? AFAIK it means that the values
are processed as a set of bit flags... As 0 doesn't change anything a value
for 0 is always included. Generally when you use the flags attribute you use
powers of 2 starting with 1...

Try :
http://msdn2.microsoft.com/en-us/library/system.flagsattribute(VS.80).aspx

---
Patrice

> I'm having issues working with some legacy enums that have multiple zero
> values.
[quoted text clipped - 22 lines]
> Cheers,
> Nathan
Nathan Baulch - 25 Apr 2007 02:10 GMT
Unfortunately the enums in question are not defined by me.
Actually, one of them is Microsoft.VisualBasic.MsgBoxStyle in the
Microsoft.VisualBasic assembly.
As you can see below, there are three zero values meaning OkOnly and
DefaultButton1 are appended when calling ToString on any non-zero value.

[Flags]
public enum MsgBoxStyle
{
   ApplicationModal = 0,
   OkOnly = 0,
   DefaultButton1 = 0,
   OkCancel = 1,
   AbortRetryIgnore = 2,
   YesNoCancel = 3,
   YesNo = 4,
   RetryCancel = 5,
   Critical = 0x10,
   Question = 0x20,
   Exclamation = 0x30,
   Information = 0x40,
   DefaultButton2 = 0x100,
   DefaultButton3 = 0x200,
   SystemModal = 0x1000,
   MsgBoxHelp = 0x4000,
   MsgBoxSetForeground = 0x10000,
   MsgBoxRight = 0x80000,
   MsgBoxRtlReading = 0x100000,
}

For the time being, I've found a workaround using a TypeConverter and some
simple string manipulation:

static void FlagsEnumTest(Type type, object value)
{
   TypeConverter converter = TypeDescriptor.GetConverter(type);
   Enum[] items = (Enum[])converter.ConvertTo(value, typeof(Enum[]));
   foreach (Enum item in items)
   {
       if (Enum.IsDefined(type, item))
       {
           string itemName = item.ToString();
           int pos = itemName.LastIndexOf(", ");
           if (pos >= 0)
           {
               itemName = itemName.Substring(pos + 2);
           }
           Console.WriteLine(itemName);
       }
       else
       {
           //contains undefined parts
       }
   }
}

> Any reason for using the flags attribute ? AFAIK it means that the values
> are processed as a set of bit flags... As 0 doesn't change anything a
[quoted text clipped - 33 lines]
>> Cheers,
>> Nathan
Peter Duniho - 25 Apr 2007 03:57 GMT
> Unfortunately the enums in question are not defined by me.
> Actually, one of them is Microsoft.VisualBasic.MsgBoxStyle in the
> Microsoft.VisualBasic assembly.
> As you can see below, there are three zero values meaning OkOnly and
> DefaultButton1 are appended when calling ToString on any non-zero value.
> [...]

What a strange problem.

On the one hand, at least part of the behavior you're seeing is "by design".
That is, when an enum has the "Flags" attribute, when it gets formatted as a
string all of the values set within the enum instance should be included in
the string.

On the other hand, it seems to me that it ought to include ALL of the zero
values, or NONE (with the latter being preferable).  Why the string
formatter should be "smart" enough to recognize that one of the zero values
should not be included, but still wind up including the others is beyond me.

Assuming your workaround is simply "proof-of-concept" and that in real usage
you would actually take each of the individual strings you create and
concatenate them into a full collection of the flags set in the enum
instance, it seems like that's a fine way of dealing with the issue.
However, it seems to me that you shouldn't have to do that, and that you
might want to consider reporting the behavior as a bug.

Pete
Patrice - 25 Apr 2007 08:57 GMT
Looks like the problem MS had is that those values are using both bit fields
for later values and "magic numbers" for lowest numbers...

Try to see what YesNoCancel would give as IMO 0 is not the only problem you
could have.

It's always better to always tell us the whole story. In this particular
case and depending on what you are trying to do, I would likely use the .NET
Framework MessageBox.Show method that doesn't mix those values together...
---
Patrice

> Unfortunately the enums in question are not defined by me.
> Actually, one of them is Microsoft.VisualBasic.MsgBoxStyle in the
[quoted text clipped - 89 lines]
>>> Cheers,
>>> Nathan

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.