简体   繁体   English

const vs. 方法参数的枚举

[英]Const Vs. Enum for Method Parameter

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. 我试图确定常量字符串是否真的比MyEnum.ToString()枚举更好的用于高性能应用程序。

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. 我了解enumerationValue.ToString()存在一些性能问题。 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. 我的问题是method参数是一个字符串,然后调用者可以传递他想要的任何内容。 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. 正如我所说,我正在寻找一种方法来吃蛋糕。

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. 我不确定,但是我会怀疑.ToString()做得很好-不用担心。 It gets called a lot! 它被称为很多!

OK, this may not be what you want, but it provides for type safety and prevents you from having to do a ToString later. 好的,这可能不是您想要的,但是它提供了类型安全性,并防止您以后必须执行ToString First set up a class w/ one property that doesn't have a setter, and a New method that does the setting: 首先,设置一个带有一个没有setter的属性的类,以及一个执行设置的New方法:

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. 它们不是恒定的,但不能修改,我认为通过适当地使用Friend可以安排它,以便其他任何人也不能创建对象。 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. 这可能是过大的杀伤力,但是对象确实倾向于使自己安全。

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. IDE可以弹出可用项目列表。

To convert your enum to string, you could have a dictionnary of (Enum, String) 要将枚举转换为字符串,可以使用(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)? 我可以在VB.NET中给枚举一个属性(就像在Java中一样)吗?

http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx http://weblogs.asp.net/stefansedich/archive/2008/03/12/enum-with-string-values-in-c.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM