Reputation: 1706
I have a seemingly very basic question. I'm trying to decide whether or not constants strings would really be better than enums with MyEnum.ToString() called for a high performance application.
I have a class, enum, method like this...
Public Enum MyEnum
MyValue1
MyValue2
End Enum
Public Class MyImportantClass
Public Sub Foo(ByVal enumerationValue As MyEnum)
' Some code in here that needs to do this at some point
Dim str As String = enumerationValue.ToString()
End Sub
End Class
I understand enumerationValue.ToString() has some performance issues. However, another developer suggested instead of using Enums, use Constant Strings. My problem is that the method parameter is then a string, and the caller can then pass whatever he wants. Not just any string will work, obviously, so this is a run-time bug.
Public Sub Foo(ByVal enumerationValue As String)
' Some code in here that needs to do this at some point
' Dim str As String = enumerationValue
End Sub
I want the safety of the enum, but the performance of a constant. As I said, I'm looking for a way to have my cake and eat it too.
Upvotes: 2
Views: 1223
Reputation: 12748
Stick with enum, it's a lot more user frendly with passing as a parameter to a function. The IDE can pop-up the list of available items.
To convert your enum to string, you could have a dictionnary of (Enum, String)
or
There's some nice articles on how to put attributes on your enum, this way you can convert it to a string.
Can I give an enum an attribute in VB.NET (like I can do in Java)?
http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx
Upvotes: 0
Reputation: 516
OK, this may not be what you want, but it provides for type safety and prevents you from having to do a ToString
later. First set up a class w/ one property that doesn't have a setter, and a New method that does the setting:
Public Class MyEnumObj
Private _Value As String
Public ReadOnly Property Value As String
Get
Return _Value
End Get
End Property
Private Sub New()
' Hide the default constructor '
End Sub
Public Sub New(inValue As String)
_Value = inValue
End Sub
End Class
Then declare your "constants":
Public MyValue1 As New MyEnumObj("MyValue1")
Public MyValue2 As New MyEnumObj("MyValue2")
They're not constant but can't be modified, and I think by proper use of Friend
you can arrange it so nobody else can create the objects, either. Now you can say:
Public Sub Foo(Optional ByVal enumerationValue As MyEnumObj = Nothing)
' Some code in here that needs to do this at some point '
Dim str As String = If(enumerationValue IsNot Nothing, enumerationValue.Value, String.Empty)
End Sub
This may be overkill, but objects do tend to lend themselves to type safety.
Upvotes: 0
Reputation: 19765
Enum is by far the way to go. Somewhat self-documenting and definately falling into the 'pit of success' becuase you'll never get bogus data.
I don't know for sure but I would suspect .ToString() does nice stuff - don't worry about it. It gets called a lot!
Upvotes: 1