我有一个vb.net枚举,如下所示:
' define enumeration for keypad states
Enum KeyPadState
KEYPAD_NO ' no keypad
KEYPAD_UC ' upper case keypad
KEYPAD_LC ' lower case keypad
KEYPAD_NU ' numeric keypad
KEYPAD_SY ' symbol keypad
End Enum
然后我定义了一个结构元素,用于将上面枚举的成员从枚举值转换为字符串值,然后再返回。声明的结构如下所示。请注意我尝试插入的成员函数。 “新”的工作正在发挥作用。
' define keypad type look up structure
Private Structure KeyPadXlat
Dim KeyPadEnum As KeyPadState
Dim KeyPadStr As String
' initializer subroutine
Public Sub New(nKeyPadEnum As KeyPadState, nKeyPadStr As String)
KeyPadEnum = nKeyPadEnum
KeyPadStr = nKeyPadStr
End Sub
' translate string to enum
Public Function ToEnum(xKeyPadStr As String) As KeyPadState
For Each item As KeyPadXlat In ????
Next
End Function
' translate enum to string
Public Function ToStr(xKeyPadEnum As KeyPadState) As String
End Function
End Structure
结构数组的实际实例如下所示及其初始化代码。
Dim KeyPadLookUp() As KeyPadXlat = { _
New KeyPadXlat(KeyPadState.KEYPAD_NO, "KEYPAD_NO"), _
New KeyPadXlat(KeyPadState.KEYPAD_UC, "KEYPAD_UC"), _
New KeyPadXlat(KeyPadState.KEYPAD_LC, "KEYPAD_LC"), _
New KeyPadXlat(KeyPadState.KEYPAD_NU, "KEYPAD_NU"), _
New KeyPadXlat(KeyPadState.KEYPAD_SY, "KEYPAD_SY") _
}
所以我的问题是关于我试图创建的成员函数来在枚举值和字符串值之间来回转换。我在这里再次复制其中一个作为参考:
' translate string to enum
Public Function ToEnum(xKeyPadStr As String) As KeyPadState
For Each item As KeyPadXlat In ????
Next
End Function
我需要帮助的是如何编写For Each循环的代码,以便在成员函数中迭代结构数组的所有元素。
答案 0 :(得分:1)
说实话,你真的不需要所有代码。这应该做得很好。
Enum KeyPadState
KEYPAD_NO ' no keypad
KEYPAD_UC ' upper case keypad
KEYPAD_LC ' lower case keypad
KEYPAD_NU ' numeric keypad
KEYPAD_SY ' symbol keypad
End Enum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim state As KeyPadState
state = KeyPadState.KEYPAD_LC
'this line will assign the name of the enum `state` to a string called `tempstring`
'It's hardly worth encapsulating it into a function so I've left it as is
'But if you want to provide consistent code, it would be better to.
Dim tempstring As String
tempstring = [Enum].GetName(GetType(KeyPadState), state)
Dim anyString As String = "KEYPAD_UC"
Dim tempState As KeyPadState
'the following line will try to parse `anyString` to an enum value of the same type as the variable to be assigned.
'In this case `state`
tempState = ParseToKeypadState(anyString)
End Sub
Private Function ParseToKeypadState(tempString As String) As KeyPadState
Dim returnValue As KeyPadState
If Not [Enum].TryParse(tempString, returnValue) Then
'handle parsing error here
End If
Return returnValue
End Function
答案 1 :(得分:0)
你的代码存在各种各样的错误。
首先,我建议你的命名惯例很差。如果您按照整个.NET Framework的方式执行操作,那么您的枚举将如下所示:
Enum KeyPadState
None
UpperCase
LowerCase
Numeric
Symbol
End Enum
这是清晰的,自我记录的。
其次,不建议使用“Xlat”之类的缩写。这对没有先验知识的人来说毫无意义。编写“翻译”是否太麻烦,然后让Intellisense在需要在代码中使用它时找到它?
至于你班级的实施,为什么它需要任何方法呢?在创建实例时,您正在传递KeyPadState
值和文本表示,那么这些方法有什么用呢?您的结构应该只是一个构造函数和两个属性:
Private Structure KeyPadStateTranslation
Public ReadOnly Property Value As KeyPadState
Public ReadOnly Property Text As String
Public Sub New(value As KeyPadState, text As String)
Me.Value = value
Me.Text = text
End Sub
End Structure
创建实例时会设置属性值,并通过属性检索它们。一切都有一个合适的名字。
那就是说,你甚至不需要提供文本,因为你可以简单地在值上调用ToString
来获取它:
Private Structure KeyPadStateTranslation
Public ReadOnly Property Value As KeyPadState
Public ReadOnly Property Text As String
Public Sub New(value As KeyPadState)
Me.Value = value
Me.Text = value.ToString()
End Sub
End Structure
此外,声明Private
结构的事实是一个问题。这表明它是在另一种类型中声明的。那是不对的。结构是一流的类型,就像类一样,因此它们属于自己的专用代码文件,就像类一样。
那个结构有什么意义呢?你仍然需要遍历你的数组来找到一个匹配值或一些文本的实例,所以它并没有真正帮助。 Dictionary
可能会更好,但如果您需要转换该值,并且需要使用ToString
或Enum.Parse
时,您也可以在值上调用.TryParse
其他方式。