連想配列


  VB + Framework ( C# )



Msdn2 のサンプル通りです。

↓VB と C# のサンプルがあります
http://msdn.microsoft.com/ja-jp/library/xfhwa508(VS.80).aspx

作成とコレクション取得・列挙の部分が違いますが、基本同じです

  
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
  

↓C#で連想配列作成
  
Dictionary<string, string> openWith = new Dictionary<string, string>();
  

↓C#で列挙
  
foreach( KeyValuePair<string, string> kvp in openWith ) {
	Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}

Dictionary<string, string>.ValueCollection valueColl = openWith.Values;

foreach( string s in valueColl ) {
	Console.WriteLine("Value = {0}", s);
}

Dictionary<string, string>.KeyCollection keyColl = openWith.Keys;

foreach( string s in keyColl ) {
	Console.WriteLine("Key = {0}", s);
}
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加




SQLの窓  天気  IT用語辞典
Yahoo!ニュース  マルチ辞書
PHP マニュアル  Google URL短縮 


フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ