2017-08-07 164 views
-1

我有一个vb.net枚举,看起来像这样:如何循环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

你不需要任何循环。 Enum类已经拥有了你需要的一切。 – jmcilhinney

回答

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 
+0

感谢您的善意和有益的答复。 –

0

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

首先,我建议你的命名约定很差。如果你做的是在整个.NET框架做了,那么你枚举应该是这样的:

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其实是一个问题。这表明它是在另一种类型中声明的。那是不对的。结构就像类一样是第一类类型,所以它们属于它们自己的专用代码文件,就像类一样。

虽然这个结构有什么意义呢?你仍然需要遍历你的数组来找到一个匹配一个值或一些文本的实例,所以它并没有真正的帮助。 A Dictionary可能会更好,但如果您需要转换该方式并使用Enum.Parse.TryParse,则您可能只需拨打ToString即可。