Enum的数值 [英] Numeric value fom Enum
本文介绍了Enum的数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助才能从我的ComboBox中的Enum by Index或Text获取=之后的数值(常量)。
提前谢谢,
Henrik
< span class =code-keyword>私有 枚举 IPCamFrameRates
<说明( Max)> Max = 0
<说明( 20\" 跨度>)GT; f20 = 1
<说明( 15\" 跨度>)GT; f15 = 3
<说明( 10\" 跨度>)GT; f10 = 6
<说明( 5帧)> f5 = 11
<说明( 4帧)> f4 = 12
<说明( 3帧)> f3 = 13
<说明( 2帧)> f2 = 14
<说明( 1帧)> f1 = 15
<说明( 1 fps / 2s)> fps1_2s = 17
<说明( 1 fps / 3s)> fps1_3s = 19
<说明( 1 fps / 4s)> fps1_4s = 21
<说明( 1 fps / 5s)> fps1_5s = 23
结束 枚举
私有 Sub cboFrameRate1_SelectedIndexChanged(sender As System。 Object ,e _
As System.EventArgs) 句柄 cboFrameRate1.SelectedIndexChanged
?????
结束 Sub
公共 共享 功能 GetEnumDescription( ByVal EnumConstant As [ Enum ]) As _
字符串
Dim fi As Reflection.FieldInfo = EnumConstant。 GetType ()。GetField(_
EnumConstant.ToString())
< span class =code-keyword> Dim attr() As System.ComponentModel.DescriptionAttribute = DirectCast (_
fi.GetCustomAttributes( GetType (_
System.ComponentModel.Descrip tionAttribute), False ),_
System.ComponentModel.DescriptionAttribute())
如果 attr.Length> 0 然后
返回 attr ( 0 )。说明
其他
返回 EnumConstant.ToString()
结束 如果
结束 功能
公共 共享 功能 GetEnumDescriptions( ByVal EnumConstant As Type) As _
String ()
' 语法:Me.cbFileActionType.Items.AddRange(GetEnumDescriptions(
' GetType(eFileListActionType)))
' http://blog.dahead.de/howto-vb-net-enums-mit-beschreibungen-versehen
< span class =code-keyword> Dim res(-1) As String
对于 每个 ec As [ 枚举] 在 [枚举]。GetValues (EnumConstant)
ReDim 保留res(UBound(res)+ 1 )
res(UBound(res))= GetEnumDescription(ec)
下一步
返回 res
结束 功能
解决方案
取代Enum,你最好使用词典 [ ^ ]。字典是键和值的集合,因此,例如,您可以使用5帧
作为键,11
作为价值:
首先,在你的构造函数中,将键/值添加到字典中:
公开 类 YourClass
私有 IPCamFrameRates 作为 新字典( 字符串,整数)()
公共 Sub 新()
IPCamFrameRates。添加( Max, 0 )
IPCamFrameRates.Add( 20, 1 )
IPCamFrameRates.Add( 15, 3 )
IPCamFrameRates.Add( 10, 6 )
IPCamFrameRates.Add( 5帧, 11 )
添加其他值
结束 Sub
要从字典中获取值,请尝试以下方法:
如果 IPCamFrameRates.ContainsKey(yourKeyFromComboBox)那么
Dim 结果作为 整数 = IPCamFrameRates(yourKeyFromComboBox)
否则
' 给定的密钥不在字典中
结束 如果
Hi,
I need help to get the numeric value (constant) after = from an Enum by Index or Text from my ComboBox.
Thank you in advance,
Henrik
Private Enum IPCamFrameRates
<Description("Max")> Max = 0
<Description("20")> f20 = 1
<Description("15")> f15 = 3
<Description("10")> f10 = 6
<Description("5 frames")> f5 = 11
<Description("4 frames")> f4 = 12
<Description("3 frames")> f3 = 13
<Description("2 frames")> f2 = 14
<Description("1 frame")> f1 = 15
<Description("1 fps/2s")> fps1_2s = 17
<Description("1 fps/3s")> fps1_3s = 19
<Description("1 fps/4s")> fps1_4s = 21
<Description("1 fps/5s")> fps1_5s = 23
End Enum
Private Sub cboFrameRate1_SelectedIndexChanged(sender As System.Object, e _
As System.EventArgs) Handles cboFrameRate1.SelectedIndexChanged
?????
End Sub
Public Shared Function GetEnumDescription(ByVal EnumConstant As [Enum]) As _
String
Dim fi As Reflection.FieldInfo = EnumConstant.GetType().GetField( _
EnumConstant.ToString())
Dim attr() As System.ComponentModel.DescriptionAttribute = DirectCast( _
fi.GetCustomAttributes(GetType( _
System.ComponentModel.DescriptionAttribute), False), _
System.ComponentModel.DescriptionAttribute())
If attr.Length > 0 Then
Return attr(0).Description
Else
Return EnumConstant.ToString()
End If
End Function
Public Shared Function GetEnumDescriptions(ByVal EnumConstant As Type) As _
String()
' SYNTAX : Me.cbFileActionType.Items.AddRange(GetEnumDescriptions(
' GetType(eFileListActionType)))
' http://blog.dahead.de/howto-vb-net-enums-mit-beschreibungen-versehen
Dim res(-1) As String
For Each ec As [Enum] In [Enum].GetValues(EnumConstant)
ReDim Preserve res(UBound(res) + 1)
res(UBound(res)) = GetEnumDescription(ec)
Next
Return res
End Function
解决方案
Instead of an Enum, you better use a Dictionary[^] for this. A dictionary is a collection of keys and values, so for example, you can use"5 frames"
as key and11
as value:
First, in your constructor, add the keys/values to the dictionary:
Public Class YourClass Private IPCamFrameRates As New Dictionary(Of String, Integer)() Public Sub New() IPCamFrameRates.Add("Max", 0) IPCamFrameRates.Add("20", 1) IPCamFrameRates.Add("15", 3) IPCamFrameRates.Add("10", 6) IPCamFrameRates.Add("5 frames", 11) ' add other values End Sub
And to get a value from the dictionary, try this:
If IPCamFrameRates.ContainsKey(yourKeyFromComboBox) Then Dim result As Integer = IPCamFrameRates(yourKeyFromComboBox) Else ' given key not in dictionary End If
这篇关于Enum的数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文