32

我正在为我的项目使用窗口应用程序。在某些情况下,我需要定义字符串枚举并在我的项目中使用它。

IE

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

    Public Enum Test
        PersonalInfo
        Contanct
    End Enum

现在我想要那个变量 PersonalInfo 和 Contract 的值作为“个人信息”和“个人联系”。

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

提前致谢...

4

7 回答 7

53

对于非整数值,可以使用Constin a Structure(or ) 代替:Class

Structure Test
    Const PersonalInfo = "Personal Info"
    Const Contanct = "Personal Contanct"
End Structure

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

Module Test
    Public Const PersonalInfo = "Personal Info"
    Public Const Contanct = "Personal Contanct"
End Module

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

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)
于 2013-04-24T19:01:01.497 回答
26

你可以创建一个新类型

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

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

Sub Main

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

End Sub

Sub DoSomething(test As Test)
    Console.WriteLine(test.ToString())
End Sub

输出:

个人联系
个人信息

于 2012-09-07T07:05:31.743 回答
11

如何使用标记。就像是:

Public Enum MyEnum
<StringValue("Personal Contact")>Contact
<StringValue("My PersonalInfo")>PersonalInfo
End Enum

您必须将 StringValue 属性编写为:

Public Class StringValueAttribute
    Inherits Attribute

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

End Class

把它弄出来:

 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

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

Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")

要获取“属性”值(为清楚起见,删除了处理“无”):

  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

以及获取字符串属性的调用(使用枚举):

Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)
于 2013-01-31T20:53:55.260 回答
5

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

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

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

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

于 2012-09-07T05:42:23.060 回答
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

用法:

'Can be anywhere in you applicaiton:
Link.xChangeCycleActive

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

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

于 2014-08-15T13:36:19.223 回答
2

如果您只想在列表或组合中显示枚举,则可以使用标记,例如

Private Enum MyEnum
    Select_an_option___
    __ACCOUNTS__
    Invoices0
    Review_Invoice
    __MEETINGS__
    Scheduled_Meetings0
    Open_Meeting
    Cancelled_Meetings0
    Current_Meetings0
End Enum

然后将 拉MyEnum入一个字符串并使用Replace(或Regex)替换标签:“___”与“...”,“__”与“**”,“_”与“”,并删除尾随数字。然后将其重新打包成一个数组并将其转储到一个组合框中,如下所示:

Select an option...
**ACCOUNTS**
Invoices
Review Invoice
**MEETINGS**
Scheduled Meetings
Open Meeting
Cancelled Meetings
Current Meetings

(例如,您可以使用这些数字来禁用用于输入发票编号或会议室的文本字段。在示例中,Review Invoice可能Open Meeting需要额外的输入,因此可能会为这些选择启用文本框。)

当您解析选定的组合项时,枚举将按预期工作,但您只需要添加一行代码 - 文本替换 - 即可使组合看起来如您所愿。

(解释大约是实际解决方案的 10 倍!)

于 2017-11-22T04:20:19.440 回答
1

微软的这项技术——“如何:确定与枚举值关联的字符串(Visual Basic) ”——在某些情况下会很有用(不幸的是它对我没有帮助:()。微软的例子:

VB:

Public Enum flavorEnum
    salty
    sweet
    sour
    bitter
End Enum

Private Sub TestMethod()
    MsgBox("The strings in the flavorEnum are:")
    Dim i As String
    For Each i In [Enum].GetNames(GetType(flavorEnum))
        MsgBox(i)
    Next
End Sub
于 2021-08-04T10:14:38.637 回答