Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.Net中定义字符串枚举_Vb.net_String_Enums_Windows Applications - Fatal编程技术网

在VB.Net中定义字符串枚举

在VB.Net中定义字符串枚举,vb.net,string,enums,windows-applications,Vb.net,String,Enums,Windows Applications,我正在为我的项目使用窗口应用程序。在某些情况下,我需要定义字符串枚举并在项目中使用它 i、 e 现在我想要变量PersonalInfo和合同的值为“PersonalInfo”和“personalicontanct” 如何使用枚举获取此值?或者以其他方式来做 先谢谢你 如何使用枚举获取此值?或者以其他方式来做 将枚举值映射到字符串有三种常见方法: 使用字典(您的枚举类型、字符串) 使用属性(例如,DescriptionAttribute)装饰枚举值,并使用反射获取它们 使用开关语句 在我看来,

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

i、 e

现在我想要变量PersonalInfo和合同的值为“PersonalInfo”和“personalicontanct”

如何使用枚举获取此值?或者以其他方式来做

先谢谢你

如何使用枚举获取此值?或者以其他方式来做

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

  • 使用
    字典(您的枚举类型、字符串)
  • 使用属性(例如,
    DescriptionAttribute
    )装饰枚举值,并使用反射获取它们
  • 使用
    开关
    语句
在我看来,第一个选择可能是最简单的

如何使用枚举获取此值?或者以其他方式来做

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

  • 使用
    字典(您的枚举类型、字符串)
  • 使用属性(例如,
    DescriptionAttribute
    )装饰枚举值,并使用反射获取它们
  • 使用
    开关
    语句

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

您可以创建一个新类型

''' <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
输出:

个人接触
个人信息


您可以创建一个新类型

''' <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
输出:

个人接触
个人信息


使用标签怎么样。比如:

Public Enum MyEnum
<StringValue("Personal Contact")>Contact
<StringValue("My PersonalInfo")>PersonalInfo
End Enum
要把它弄出来:

 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")
Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)
要获取“属性”值(为了清晰起见,删除了“无”处理):

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

Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")
Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)

使用标签怎么样。比如:

Public Enum MyEnum
<StringValue("Personal Contact")>Contact
<StringValue("My PersonalInfo")>PersonalInfo
End Enum
要把它弄出来:

 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")
Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)
要获取“属性”值(为了清晰起见,删除了“无”处理):

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

Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")
Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)

对于非整数值,可以使用
结构
(或
)中的
常量

Structure Test
    Const PersonalInfo = "Personal Info"
    Const Contanct = "Personal Contanct"
End Structure
或者在
模块
中,无需
测试即可直接访问。
部分:

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)

对于非整数值,可以使用
结构
(或
)中的
常量

Structure Test
    Const PersonalInfo = "Personal Info"
    Const Contanct = "Personal Contanct"
End Structure
或者在
模块
中,无需
测试即可直接访问。
部分:

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)

我知道这是一篇老帖子,我发现了一个很好的解决方案,值得分享:

''' <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
这可以防止不必要的额外编码,易于维护,我认为这可以最大限度地减少额外的处理器开销

另外,visual studio会在键入“Link”后立即显示字符串属性
就像是一个普通的枚举一样

我知道这是一个老帖子,我找到了一个很好的解决方案,值得分享:

''' <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
这可以防止不必要的额外编码,易于维护,我认为这可以最大限度地减少额外的处理器开销

另外,visual studio会在键入“Link”后立即显示字符串属性
就像它是一个常规枚举一样

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

Private Enum MyEnum
    Select_an_option___
    __ACCOUNTS__
    Invoices0
    Review_Invoice
    __MEETINGS__
    Scheduled_Meetings0
    Open_Meeting
    Cancelled_Meetings0
    Current_Meetings0
End Enum
然后将
MyEnum
拉入字符串,并使用
Replace
(或
Regex
)替换标记:“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。然后将其重新打包到一个数组中,并将其转储到一个组合框中,该组合框如下所示:

Select an option...
**ACCOUNTS**
Invoices
Review Invoice
**MEETINGS**
Scheduled Meetings
Open Meeting
Cancelled Meetings
Current Meetings
(您可以使用这些数字来禁用用于输入发票号码或会议室的文本字段。在本例中,
查看发票
打开会议
可能需要额外的输入,因此可以为这些选择启用文本框。)

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


(解释是实际解决方案的10倍左右!)

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

Private Enum MyEnum
    Select_an_option___
    __ACCOUNTS__
    Invoices0
    Review_Invoice
    __MEETINGS__
    Scheduled_Meetings0
    Open_Meeting
    Cancelled_Meetings0
    Current_Meetings0
End Enum
然后将
MyEnum
拉入字符串,并使用
Replace
(或
Regex
)替换标记:“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。然后将其重新打包到一个数组中,并将其转储到一个组合框中,该组合框如下所示:

Select an option...
**ACCOUNTS**
Invoices
Review Invoice
**MEETINGS**
Scheduled Meetings
Open Meeting
Cancelled Meetings
Current Meetings
(您可以使用这些数字来禁用用于输入发票号码或会议室的文本字段。在本例中,
查看发票
打开会议
可能需要额外的输入,因此可以为这些选择启用文本框。)

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


(解释是实际解决方案的10倍左右!)

谢谢jon。事实上,我是windows应用程序新手。你能给我简单介绍一下first way(字典…)吗?@BrijeshPatel:Stack Overflow不是从头开始学习的好方法。我建议您阅读
词典
文档,最好阅读一本涵盖收藏的VB好书。谢谢jon。事实上,我是windows应用程序新手。你能给我简单介绍一下first way(字典…)吗?@BrijeshPatel:Stack Overflow不是从头开始学习的好方法。我建议您阅读
字典
文档,最好阅读一本关于集合的VB好书。如何获取字符串常量?对于字符串枚举来说太复杂了。对某些人来说,它可能看起来很复杂,但我认为这是IMH的最佳解决方案