2010年11月16日 星期二

'Dictionary Sample 2

'Dictionary Sample 2
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim str1 = ""
        Dim dict1 As New Dictionary(Of String, String)

        dict1.Add("A", "優")
        dict1.Add("B", "佳")
        dict1.Add("C", "可")
        dict1.Add("D", "差")

        str1 = str1 & vbNewLine
        'KeyValuePair
        For Each kvp As KeyValuePair(Of String, String) In dict1
            str1 = str1 & kvp.Key & Space(3) & kvp.Value & vbNewLine
        Next kvp

        'assign value
        dict1("D") = "劣"

        'ValueCollection
        Dim valueColl As Dictionary(Of String, String).ValueCollection = dict1.Values
        str1 = str1 & vbNewLine
        For Each s As String In valueColl
            str1 = str1 & "Value = " & s & vbNewLine
        Next s

        'KeyCollection
        Dim keyColl As Dictionary(Of String, String).KeyCollection = dict1.Keys

        str1 = str1 & vbNewLine
        For Each s As String In keyColl
            str1 = str1 & "Key = " & s & vbNewLine
        Next s

        'Query
        str1 = str1 & vbNewLine
        str1 = str1 & dict1("C") & vbNewLine

        'Remove
        str1 = str1 & vbNewLine & "Remove(""C"")"

        dict1.Remove("C")

        'ContainsKey
        str1 = str1 & vbNewLine
        If Not dict1.ContainsKey("C") Then
            str1 = str1 & "Key ""C"" is not found."
        End If
        MsgBox(str1)
    End Sub
End Class

沒有留言: