|
Imports System
Imports System.Collections.Generic
Module Example
Sub Main()
Dim openWith As New Dictionary(Of String, String)
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' **************************************************
' 同一キーで Add はエラー
' **************************************************
Try
openWith.Add("txt", "winword.exe")
Catch
Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
' **************************************************
' 参照
' **************************************************
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' **************************************************
' 置き換え
' **************************************************
openWith("rtf") = "winword.exe"
Console.WriteLine("For key = ""rtf"", value = {0}.", _
openWith("rtf"))
' **************************************************
' 追加も可
' **************************************************
openWith("doc") = "winword.exe"
' **************************************************
' 無いとエラー
' **************************************************
Try
Console.WriteLine("For key = ""tif"", value = {0}.", _
openWith("tif"))
Catch
Console.WriteLine("Key = ""tif"" is not found.")
End Try
' **************************************************
' 別の参照方法( 戻り値は bool )
' **************************************************
Dim value As String = ""
If openWith.TryGetValue("tif", value) Then
Console.WriteLine("For key = ""tif"", value = {0}.", value)
Else
Console.WriteLine("Key = ""tif"" is not found.")
End If
' **************************************************
' 存在チェック
' **************************************************
If Not openWith.ContainsKey("ht") Then
openWith.Add("ht", "hypertrm.exe")
Console.WriteLine("Value added for key = ""ht"": {0}", _
openWith("ht"))
End If
' **************************************************
' 列挙
' **************************************************
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
' **************************************************
' 値のコレクションを取得
' **************************************************
Dim valueColl As _
Dictionary(Of String, String).ValueCollection = _
openWith.Values
' **************************************************
' 列挙
' **************************************************
Console.WriteLine()
For Each s As String In valueColl
Console.WriteLine("Value = {0}", s)
Next s
' **************************************************
' キーのコレクションを取得
' **************************************************
Dim keyColl As _
Dictionary(Of String, String).KeyCollection = _
openWith.Keys
' **************************************************
' 列挙
' **************************************************
Console.WriteLine()
For Each s As String In keyColl
Console.WriteLine("Key = {0}", s)
Next s
' **************************************************
' 削除
' **************************************************
Console.WriteLine(vbLf + "Remove(""doc"")")
openWith.Remove("doc")
' **************************************************
' クリア
' **************************************************
Console.WriteLine(vbLf + "{0}", openWith.Count )
openWith.Clear()
Console.WriteLine("{0}", openWith.Count )
End Sub
End Module
| |