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.
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