VB.net如何将此状态集合输入转换为字典键值输入表单

时间:2016-08-31 13:11:40

标签: vb.net dictionary collections

Dim states As Collection = New Collection()

Sub Output(Value As String)

    txtOutput.Text += Value + vbCrLf

End Sub

Sub ClearOutput(sender As Object, e As RoutedEventArgs) Handles btnClear.Click
    txtOutput.Text = ""
    txtInput.Text = ""
    states.Clear()
End Sub

Sub btnAdd_Click(sender As Object, e As RoutedEventArgs) Handles btnAdd.Click

    Dim input As String = txtInput.Text

    states.Add(input)
    Output("You added: " + input)

End Sub

Private Sub btnGet_Click(sender As Object, e As RoutedEventArgs) Handles btnGet.Click

    Dim counter As Integer = 1
    Dim stateID As String = txtInput.Text.ToString

    If CStr(states.Count) = 0 Then
        Output("Not found")
    End If

    For Each state As String In states
        If states(counter).Contains(stateID) Then
            If Not states(counter).Contains(",") Then
                Output("Add a valid state entry e.g. California, CA")
                Exit For
            End If
            Dim stateOnly = states(counter).Substring(0, state.IndexOf(","))
            Output("You requested: " + states(counter))
            Exit For

        End If

        counter = counter + 1
        If counter > CStr(states.Count) Then
            Output("Not found")
        End If
    Next

End Sub

Private Sub btnRemove_Click(sender As Object, e As RoutedEventArgs) Handles btnRemove.Click

    Dim counter As Integer = 1
    Dim stateID As String = txtInput.Text.ToString
    Dim firstCount As Integer = CStr(states.Count)

    If CStr(states.Count) = 0 Then
        Output("Not found")
    End If
    For Each state As String In states
        If states(counter).Contains(stateID) Then
            If Not states(counter).Contains(",") Then
                Output("Add a valid state entry e.g. California, CA")
                Exit For
            End If
            states.Remove(counter)
            txtOutput.Text = ""

            Dim secondCount As Integer = CStr(states.Count)

            If secondCount < firstCount And secondCount > 0 Then
                counter = 0
                Output(stateID + " removed; here's what's left:")
                OutputStates()
                Exit For
            End If
            If secondCount = 0 Then
                Output("Nothing left.")

            End If
        End If

        counter = counter + 1
        If counter > CStr(states.Count) Then
            Output("Not found")
        End If
    Next

End Sub

Sub btnShow_Click(sender As Object, e As RoutedEventArgs) Handles btnShow.Click
    If CStr(states.Count) = 0 Then
        Output("No entries yet")
    End If
    OutputStates()
End Sub

Sub OutputStates()
    For Each state As String In states
        Output(state)
    Next
End Sub

我已经尝试过以下代码,但它不起作用。我在Sub OutputStates()上有一个错误,当使用字典时,输出(状态)不再起作用。 我想我需要修改输入,进入stateID和stateName,但我不知道如何

Dim states As Dictionary(Of String, String) _
    = New Dictionary(Of String, String)

Sub Output(Value As String)

    txtOutput.Text += Value + vbCrLf

End Sub

Sub ClearOutput(sender As Object, e As RoutedEventArgs) Handles btnClear.Click
    txtOutput.Text = ""
    txtInput.Text = ""
    states.Clear()
End Sub

Sub btnAdd_Click(sender As Object, e As RoutedEventArgs) Handles btnAdd.Click

    Dim input As String = txtInput.Text
    Dim length As Integer = input.Length - 1
    Dim stateID As String = input.Substring(0, 1)
    Dim stateName As String = input.Substring(2, length)

    states.Add(stateID, stateName)
    Output("You added: " + input)

End Sub

Private Sub btnGet_Click(sender As Object, e As RoutedEventArgs) Handles btnGet.Click

    Dim counter As Integer = 1
    Dim stateID As String = txtInput.Text.ToString

    If CStr(states.Count) = 0 Then
        Output("Not found")
    End If

    For Each state In states
        If states(counter).Contains(stateID) Then
            If Not states(counter).Contains(",") Then
                Output("Add a valid state entry e.g. California, CA")
                Exit For
            End If
            Dim stateOnly = states(counter).Substring(0, states(counter).IndexOf(","))
            Output("You requested: " + states(counter))
            Exit For

        End If

        counter = counter + 1
        If counter > CStr(states.Count) Then
            Output("Not found")
        End If
    Next

End Sub

Private Sub btnRemove_Click(sender As Object, e As RoutedEventArgs) Handles btnRemove.Click

    Dim counter As Integer = 1
    Dim stateID As String = txtInput.Text.ToString
    Dim firstCount As Integer = CStr(states.Count)

    If CStr(states.Count) = 0 Then
        Output("Not found")
    End If
    For Each state In states
        If states(counter).Contains(stateID) Then
            If Not states(counter).Contains(",") Then
                Output("Add a valid state entry e.g. California, CA")
                Exit For
            End If
            states.Remove(counter)
            txtOutput.Text = ""

            Dim secondCount As Integer = CStr(states.Count)

            If secondCount < firstCount And secondCount > 0 Then
                counter = 0
                Output(stateID + " removed; here's what's left:")
                OutputStates()
                Exit For
            End If
            If secondCount = 0 Then
                Output("Nothing left.")

            End If
        End If

        counter = counter + 1
        If counter > CStr(states.Count) Then
            Output("Not found")
        End If
    Next

End Sub

Sub btnShow_Click(sender As Object, e As RoutedEventArgs) Handles btnShow.Click
    If CStr(states.Count) = 0 Then
        Output("No entries yet")
    End If
    OutputStates()
End Sub

Sub OutputStates()
    For Each state In states
        Output(states)
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

在您的Sub outputStates中,您尝试遍历您的状态,但现在这是一个字典而不是普通的集合。

对于循环字典,您可以使用键值对:

Sub OutputStates()
For Each state As KeyValuePair(Of String, String) In states
    Output(state.key)
    Output(state.value)
Next

End Sub

关于分割CA, California

的其他问题

如果您假设键和值之间的分割是逗号:

Dim splitString as String = "CA, California"
Dim key As String = splitString.Split(",")(0).trim()
Dim value As String = splitString.Split(",")(1).trim()