The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,570 other followers

Archive for June 13th, 2012

.NET Enums enumerated: System.ArgumentException was unhandled by user code Message=An item with the same key has already been added.

Posted by jpluimers on 2012/06/13

All .NET languages have a cool feature: enumerated types, for instance enum in C#Enum in VB.NET, or the enum in the  Oxygene language of Delphi Prism. You can even specify the underlying integral type.

It allows you to name values, which can make your code very readable.

It also allows you to assign an integer to each of those values, which allows you to map them to existing integers in the ‘real’ world. This is both a powerful and potentially uncool feature of enums (the other uncool feature is that though you can have bitflags in Enums, most .NET languages don’t have a Set type. You need Set Extensions to do things like Set operations on Card Suit bitflags using C# extension methods).

Because of my background in the 80s in MuSimp and Turbo Pascal, I’ve done quite a bit of enums and sets in the past, hence the mentioned C# enum extension methods mentioned above. While writing this article I also found out Extending’ the Enum Class to Validate the Enum Values with the Flags Attribute that mentions quite a bit of stuff that is complementary to my code and what you will see below.

The risk of assigning integer values on C# enum is that you can assign the same integer value to multiple enum elements.

For instance, this code will fail: Read the rest of this entry »

Posted in .NET, C#, C# 2.0, C# 3.0, C# 4.0, Development, Prism, Software Development, VB.NET | Leave a Comment »

 
%d bloggers like this: