在VB.Net中定义String ENUM

Bri*_*tel 26 vb.net string enums windows-applications

我正在为我的项目使用Window Application.有一种情况我需要定义字符串枚举并在我的项目中使用它.

Dim PersonalInfo As String = "Personal Info"
Dim Contanct As String = "Personal Contanct"

    Public Enum Test
        PersonalInfo
        Contanct
    End Enum
Run Code Online (Sandbox Code Playgroud)

现在我希望将该变量PersonalInfo和Contract的值视为"个人信息"和"个人信号".

如何使用ENUM获得此值?或任何其他方式来做到这一点.

提前致谢...

Sla*_*lai 46

对于非整数值,Const在一个Structure(或Class)可以用来代替:

Structure Test
    Const PersonalInfo = "Personal Info"
    Const Contanct = "Personal Contanct"
End Structure
Run Code Online (Sandbox Code Playgroud)

或者在Module没有该Test.部分的情况下直接访问:

Module Test
    Public Const PersonalInfo = "Personal Info"
    Public Const Contanct = "Personal Contanct"
End Module
Run Code Online (Sandbox Code Playgroud)

在某些情况下,变量名称可以用作值:

Enum Test
    Personal_Info
    Personal_Contanct
End Enum

Dim PersonalInfo As String = Test.Personal_Info.ToString.Replace("_"c, " "c)

' or in Visual Studio 2015 and newer:
Dim Contanct As String = NameOf(Test.Personal_Contanct).Replace("_"c, " "c)
Run Code Online (Sandbox Code Playgroud)

  • 我更喜欢这个类的原因是因为这些枚举永远不会被实例化.它们只是在一个地方聚集在一起的字符串的枚举,然后用点符号在别处引用.请注意,我们不会声明此结构的任何实例.它只是用于包含常量的结构.你可以对一个班级做同样的事情.我也看到人们只用课堂阅读.但这些是字符串常量.所以我们使用const. (3认同)

slo*_*oth 21

你可以创建一个新类型

''' <completionlist cref="Test"/>
Class Test

    Private Key As String

    Public Shared ReadOnly Contact  As Test = New Test("Personal Contanct")
    Public Shared ReadOnly PersonalInfo As Test = New Test("Personal Info")

    Private Sub New(key as String)
        Me.Key = key
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Key
    End Function
End Class
Run Code Online (Sandbox Code Playgroud)

当你使用它时,它看起来像一个枚举:

Sub Main

    DoSomething(Test.Contact)
    DoSomething(Test.PersonalInfo)

End Sub

Sub DoSomething(test As Test)
    Console.WriteLine(test.ToString())
End Sub
Run Code Online (Sandbox Code Playgroud)

输出:

个人Contanct
个人信息

  • 结构解决方案(由 Slai)在 4 行代码中完成了同样的事情。 (2认同)

Den*_*nis 7

如何使用标记.就像是:

Public Enum MyEnum
<StringValue("Personal Contact")>Contact
<StringValue("My PersonalInfo")>PersonalInfo
End Enum
Run Code Online (Sandbox Code Playgroud)

您必须将StringValue属性编写为:

Public Class StringValueAttribute
    Inherits Attribute

    Public Property Value As String
    Public Sub New(ByVal val As String)
        Value = val
    End Sub

End Class
Run Code Online (Sandbox Code Playgroud)

要搞清楚:

 Public Function GetEnumByStringValueAttribute(value As String, enumType As Type) As Object
    For Each val As [Enum] In [Enum].GetValues(enumType)
        Dim fi As FieldInfo = enumType.GetField(val.ToString())
        Dim attributes As StringValueAttribute() = DirectCast(fi.GetCustomAttributes(GetType(StringValueAttribute), False), StringValueAttribute())
        Dim attr As StringValueAttribute = attributes(0)
        If attr.Value = value Then
            Return val
        End If
    Next
    Throw New ArgumentException("The value '" & value & "' is not supported.")
End Function

Public Function GetEnumByStringValueAttribute(Of YourEnumType)(value As String) As YourEnumType
    Return CType(GetEnumByStringValueAttribute(value, GetType(YourEnumType)), YourEnumType)
End Function
Run Code Online (Sandbox Code Playgroud)

然后调用获取Enum(使用字符串属性):

Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")
Run Code Online (Sandbox Code Playgroud)

要获取"属性"值(为清晰起见,删除处理'无'):

  Public Function GetEnumValue(Of YourEnumType)(p As YourEnumType) As String
        Return DirectCast(Attribute.GetCustomAttribute(ForValue(p), GetType(StringValueAttribute)), StringValueAttribute).Value
  End Function

  Private Function ForValue(Of YourEnumType)(p As YourEnumType) As MemberInfo
        Return GetType(YourEnumType).GetField([Enum].GetName(GetType(YourEnumType), p))
  End Function
Run Code Online (Sandbox Code Playgroud)

并调用获取字符串属性(使用Enum):

Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)
Run Code Online (Sandbox Code Playgroud)

  • 对于字符串枚举来说太复杂了. (2认同)

Jon*_*eet 5

如何使用ENUM获得此值?或任何其他方式来做到这一点.

将枚举值映射到字符串有三种常用方法:

  • 用一个 Dictionary(Of YourEnumType, String)
  • 使用属性(例如DescriptionAttribute)装饰枚举值并使用反射获取它们
  • 使用Switch声明

在我看来,这些选项中的第一个可能是最简单的.

  • @BrijeshPatel:Stack Overflow并不是一个从头学习的好方法.我建议你阅读`Dictionary`文档,理想情况下阅读一本应该涵盖馆藏的好书. (6认同)

Rob*_*ers 5

我知道这是一篇旧帖子,我找到了一个值得分享的不错的解决方案:

''' <summary>
''' Gives acces to strings paths that are used often in the application
''' </summary>
Public NotInheritable Class Link        
    Public Const lrAutoSpeed As String          = "scVirtualMaster<.lrAutoSpeed>"
    Public Const eSimpleStatus As String        = "scMachineControl<.eSimpleStatus>"
    Public Const xLivebitHMI As String          = "scMachineControl<.xLivebitHMI>"      
    Public Const xChangeCycleActive As String   = "scMachineControl<.xChangeCycleActive>"

End Class
Run Code Online (Sandbox Code Playgroud)

用法:

'Can be anywhere in you applicaiton:
Link.xChangeCycleActive
Run Code Online (Sandbox Code Playgroud)

这可以防止不必要的额外编码,易于维护,我认为这可以最大限度地减少额外的处理器开销。

此外,visual studio 在您键入“Link”后立即显示字符串属性,就像它是常规 Enum 一样