![]() |
Languages »
VB.NET »
General
Intermediate
StringEnum That Works Like System.EnumBy N JonesA base class for closely simulating enum behavior with underlying type of string. |
VB 8.0, Windows, .NET 2.0VS2005, Dev, Design
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
If you've ever had a list of string constants to maintain, use, and compare, you've probably experienced frustration at the fact that even at .NET 2.0 we don't have a native Enum for strings. Further, you can't inherit from the System.Enum class and so can't extend it to support strings.
A StringEnum would be very handy in defining possible database values, holding an enumerable list of string values for populating lists, etc. Many workarounds have been created, but they all have their issues.
Just a warning: the implementation may be a little rough since I just wanted to get something out for people to see and start using right away. That said, feedback is welcome.
I've seen several different methods of achieving something like a true String Enum. They are as follows:
Enum and then just use the ToString and Parse functions to convert to and from strings?ToString and Parse functions every time you want to work with the string value. Also, all sorting happens by numeric value and the Enum class cannot be extended.Enum. (A good example of this can be found here.) Enum, advantages are all the advantages of the Enum--can get a list of values, can parse name from string, etc.Enum, and a ".". Also, the base type is still a number and so assignments and comparisons are still done on a numeric level. Option Compare or lots of StrComp for case sensitivity. Also there is no type checking and no unnamed values are allowed. ReadOnly string values. Might as well just pass around a reference to a class.The StringEnumBase provides an inheritable base that provides functionality comparable to the System.Enum class.
It has the following advantages:
System.ComponentModel.Description and get it through the base class' .ToDesc function.System.Enum class (e.g. GetValues, Parse, CompareTo, GetNames, etc.).Inherit it in the following manner, supplying the type name of the inheritor as the generic type. This is so that the base class has a way to get the type of the class thats inheriting--used to get attributes, fields, etc. You must also declare the parameterized constructor so that it can remain private and parameterized.
Add the completionlist XML comment to get the Intellisense pop-up. The StringEnumRegisteredOnly attribute is one of the optional attributes to modify the behavior of the StringEnumBase.
''' <completionlist cref="Numbers" />
<StringEnumRegisteredOnly()> _
Public Class Numbers
Inherits StringEnumBase(Of Numbers)
Private Sub New(ByVal StrValue As String)
MyBase.New(StrValue)
End Sub
...
You can then enumerate a value as a Shared ReadOnly field or property. I prefer using a field since it fits on one line. You can also use the DescriptionAttribute in the declaration.
<Description("This is test value one.")> _
Public Shared ReadOnly One As New Numbers("ONE")
<Description("This is test value two.")> _
Public Shared ReadOnly Property Three() As Numbers
Get
Return New Numbers("TWO")
End Get
End Property Looks like some parts of .NET are still rough, even after so many years--especially in VB.NET. For example, in the DebuggerDisplayAttribute the postfix "nq" is documented as stripping the quotes from the value that it postfixes. The only problem is that it works only for C#. It looks like the inline conditional also works only for C#.
I also stumbled across the XML comment completionlist. Why isn't this incredibly handy feature documented anywhere?
2007-05-02 - Initial release
2007-05-22 - Revision to be more in line with expected behavior and to add a little functionality.
StringEnum is compared directly to a string or another object that can be converted to a string (including a different type of StringEnum). This allows for comparison against a string without the necessity of converting the string to the StringEnum type which might cause an error if the StringEnum is set to accept only registered values. StringEnum before the StringEnum will be converted to a string allowing us in some situations to control case-sensitivity. IConvertible interface so that the StringEnum will be automatically converted to a string like, for example, when it is passed to a function that is expecting a string argument. This is important since CType is not automatically called in every situation.GetValues() function to return a typed array rather than the generic System.Array.StringEnum value.StringEnum is using the behavior-modifying attributes.| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 22 May 2007 Editor: |
Copyright 2007 by N Jones Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |