如何使用成员函数迭代 VB.net 中的结构数组成员

How To Iterate Over Members of a Structure Array in VB.net Using Member Function

我有一个 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

然后我定义了一个结构元素,用于将上述枚举的成员从枚举值转换为字符串值,然后再转换回来。声明的结构如下所示。请注意我尝试插入的成员函数。 "New" 一个正在工作。

' 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 循环的代码,以便它在成员函数中遍历结构数组的所有元素。

你的代码有各种各样的错误。

首先,我认为您的命名约定很糟糕。如果您按照整个 .NET Framework 中的做法进行操作,那么您的枚举将如下所示:

Enum KeyPadState
    None
    UpperCase
    LowerCase
    Numeric
    Symbol
End Enum

这很清楚并且可以自我记录。

其次,不建议使用"Xlat"这样的缩写。这对任何没有先验知识的人来说毫无意义。写"Translate"然后在代码中需要用到的时候让Intellisense找就这么麻烦吗?

至于您的 class 的实现,为什么它根本不需要任何方法?您在创建实例时传入了 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 也是一个问题。这表明它是在另一种类型中声明的。那是不对的。结构体是 first-class 类型,就像 classes,所以它们属于自己专用的代码文件,就像 classes.

那个结构到底有什么意义呢?您仍然必须遍历数组以找到与值或某些文本匹配的实例,因此它并没有真正的帮助。 Dictionary 可能更好,但如果您需要以这种方式转换并使用 Enum.Parse.TryParse 时,您也可以只对值调用 ToString其他方式。

老实说,您真的不需要那么多代码。这应该做得很好。

    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