我正在为我的项目使用Window Application。我需要定义字符串枚举并在我的项目中使用它。
即
Dim PersonalInfo As String = "Personal Info"
Dim Contanct As String = "Personal Contanct"
Public Enum Test
PersonalInfo
Contanct
End Enum
现在我希望将该变量PersonalInfo和Contract的值视为“个人信息”和“个人信号”。
如何使用ENUM获取此值?或其他任何方式。
提前致谢...
答案 0 :(得分:44)
对于非整数值,可以使用Const
(或Structure
)中的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)
答案 1 :(得分:20)
您可以创建一个新类型
''' <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
输出:
个人信号
个人信息
答案 2 :(得分:7)
如何使用标记。类似的东西:
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
然后调用获取Enum(使用字符串属性):
Dim mEnum as MyEnum = GetEnumByStringValueAttribute(Of MyEnum)("Personal Contact")
要取出“属性”值(为清晰起见,删除处理'Nothing'):
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
调用获取字符串属性(使用Enum):
Dim strValue as String = GetEnumValue(Of MyEnum)(MyEnum.Contact)
答案 3 :(得分:5)
如何使用ENUM获取此值?或其他任何方式。
将枚举值映射到字符串有三种常用方法:
Dictionary(Of YourEnumType, String)
DescriptionAttribute
)装饰枚举值,并使用反射Switch
声明在我看来,这些选项中的第一个可能是最简单的。
答案 4 :(得分:4)
我知道这是一个老帖子我找到了一个值得分享的好解决方案:
''' <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在您输入“链接”后立即显示字符串属性 就像它是一个常规的枚举
答案 5 :(得分: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
)替换标记:“___”替换为“...”,“__”替换为“* *“,”_“with”“,并删除尾随数字。然后将其重新打包成阵列并将其转储到组合框中,该组合框看起来像:
Select an option...
**ACCOUNTS**
Invoices
Review Invoice
**MEETINGS**
Scheduled Meetings
Open Meeting
Cancelled Meetings
Current Meetings
(您可以使用这些数字来禁用文本字段来输入发票号或会议室。在示例中,Review Invoice
和Open Meeting
可能需要额外的输入,因此文本框可能会为这些选择启用。)
当您解析所选的组合项时,枚举将按预期工作,但您只需要添加一行代码 - 文本替换 - 以使组合看起来如您所愿。
(解释大约是实际解决方案的10倍!)
答案 6 :(得分:0)
来自微软的这项技术 - “How to: Determine the String Associated with an Enumeration Value (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