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
我已经尝试了下面的代码,但它不工作。我在子OutputStates()上有一个错误,当字典被使用时,输出(状态)不再工作。我想我需要修剪输入,进入statid和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
在Sub outputStates
中,您尝试遍历状态,但现在这是一个字典而不是普通的集合。
对于遍历字典,您可以使用键值对:
Sub OutputStates()
For Each state As KeyValuePair(Of String, String) In states
Output(state.key)
Output(state.value)
Next
结束子
关于CA, California
分裂的另一个问题:
如果您假设键和值之间的分隔是逗号:
Dim splitString as String = "CA, California"
Dim key As String = splitString.Split(",")(0).trim()
Dim value As String = splitString.Split(",")(1).trim()