.NET Enums
I like Enum. I love how you can define an Enum like this:
Enum ColorType Red Green Blue End Enum
Then use it as a parameter type like this:
Public Sub ProcessColor(color As ColorType) End Sub
And when you go to use it in a method you get intellisense like this:
Every so often I come across a data model that uses strings to signify types. Like an 'R' for Red and 'G' for Green and a 'B' for Blue. I would love to use my good friend Enum and go:
Enum ColorType Red = "R" Green = "G" Blue = "B" End Enum
But unfortunately this is not possible. If you are like me and wish you could do this please go here: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2161129-string-enum-for-both-c-and-vb and Vote now.
Until we get enough votes to get this implemented my current favorite alternative is to define a class like this:
Class ColorType Private Sub New(aColorType As String) Value = aColorType End Sub Public Shared ReadOnly Property Red() As ColorType Get Return New ColorType("R") End Get End Property Public Shared ReadOnly Property Green() As ColorType Get Return New ColorType("G") End Get End Property Public Property Value() As String Public Shared ReadOnly Property Blue As ColorType Get Return New ColorType("B") End Get End Property Public Shared Operator <>(object1 As ColorType, object2 As ColorType) As Boolean Return Not object1.Value.Equals(object2.Value) End Operator Public Shared Operator =(object1 As ColorType, object2 As ColorType) As Boolean Return object1.Value.Equals(object2.Value) End Operator Public Shared Widening Operator CType(aColorType As ColorType) As String Return aColorType.Value End Operator End Class
Unless there is a lot of logic around the different types then it might be good for a Replace Type Code refactoring like: http://www.refactoring.com/catalog/replaceTypeCodeWithSubclasses.html
Comments
Post a Comment