Dictionary オブジェクト | |||||||||||||
作成可能 | |||||||||||||
はい |
|||||||||||||
ライブラリ | |||||||||||||
Microsoft Scripting Runtime |
|||||||||||||
説明 | |||||||||||||
Dictionary オブジェクトは Collection オブジェクトに似ていますが、Perl 連想配列に大まかに基づいていることが異なります。Dictionary オブジェクトは、配列や Collection オブジェクトのように、データを含む項目またはメンバーと呼ばれる要素を保持します。また、オブジェクトや他の Dictionary オブジェクトなど、どのようなデータでも含むことができます。配列を処理するときのように項目の順序を使用するのではなく、データと共に格納されている固有のキー (または名前付き値) を使用して、これらの辞書項目の値にアクセスします。このため、固有の名前付き値に関連付けられているデータにアクセスする必要があるときに、Dictionary オブジェクトは最適です。 Dictionary オブジェクトに格納されている各項目にアクセスするには、For Each ...Next 構造を使用します。ただし、Dictionary オブジェクトは、その中に格納されているデータ値が含まれたバリエーションを返すのではなく、メンバーに関連付けられているキーが含まれたバリエーションを返します。次の例のように、このキーを Item メソッドに渡してメンバーを取り出します。 Dim vKey Dim sItem, sMsg Dim oDict Set oDict = CreateObject("Scripting.Dictionary") oDict.Add "One", "Engine" oDict.Add "Two", "Wheel" oDict.Add "Three", "Tire" oDict.Add "Four", "Spanner" For Each vKey In oDict sItem = oDict.Item(vKey) sMsg = sMsg & sItem & vbCrLf Next MsgBox sMsg |
|||||||||||||
Dictionary オブジェクトのプロパティ | |||||||||||||
Dictionary オブジェクトには次の 4 つのプロパティが含まれます。
|
|||||||||||||
Dictionary オブジェクトのメソッド | |||||||||||||
Dictionary オブジェクトは、次の 5 つのメソッドをサポートします。
|
Dictionary.Add メソッド | |
構文 | |
dictionaryobject.Add key, item
|
|
説明 | |
キーおよび関連付けられている項目を、指定された Dictionary オブジェクトに追加します。 |
|
一目でわかる規則 | |
|
|
例 | |
例では、Dictionary オブジェクトを使用して、状態の略語と対応する状態名を保存します。 Dim StateCode, StateName Dim StateDict Dim Key Set StateDict = CreateObject("Scripting.Dictionary") StateCode = "CA" StateName = "California" StateDict.Add StateCode, StateName StateCode = "NY" StateName = "New York" StateDict.Add StateCode, StateName StateCode = "MI" StateName = "Michigan" StateDict.Add StateCode, StateName Key = "NY" MsgBox StateDict.Item(Key) |
|
プログラミングのヒント | |
|
|
関連項目 | |
Dictionary.Key プロパティ |
|
Dictionary.CompareMode プロパティ | |
データタイプ | |
Long |
|
説明 | |
Dictionary オブジェクトのキーを比較するために使用するモードを設定または返します。 |
|
一目でわかる規則 | |
|
|
プログラミングのヒント | |
|
|
Dictionary.Count プロパティ | |
データタイプ | |
Long |
|
説明 | |
Dictionary オブジェクトの「キー/項目」ペアの数を返す、読み取り専用プロパティ。 |
|
一目でわかる規則 | |
このプロパティは、辞書の実際の項目数を返します。そのため、Count プロパティを使用して辞書の項目を繰り返す場合は、次のようなコードを使用します。 Dim ctr For ctr = 0 to dictionary.Count - 1 ' do something Next |
|
Dictionary.Exists メソッド | |
構文 | |
dictionaryobject.Exists(key)
|
|
戻り値 | |
Boolean |
|
説明 | |
指定されたキーが Dictionary オブジェクトにあるかどうかを調べます。 |
|
一目でわかる規則 | |
指定されたキーが Dictionary オブジェクトに存在する場合は True を返し、存在しない場合は False を返します。 |
|
プログラミングのヒント | |
|
|
例 | |
If oDict.Exists(strOldKey) Then oDict.Key(strOldKey) = strNewKey End If |
|
Dictionary.Item プロパティ | |
構文 | |
項目を設定する構文は次のとおりです。 dictionaryobject.Item(key) = item 項目を返す構文は次のとおりです。 value = dictionaryobject.Item(key)
|
|
データタイプ | |
任意 |
|
説明 | |
Dictionary オブジェクトの指定されたキーにリンクするデータ項目を設定または返します。 |
|
一目でわかる規則 | |
|
|
プログラミングのヒント | |
|
|
例 | |
次の例では、ユーザーが入力した状態コードに対応する状態名を取り出すための参照テーブルとして Dictionary オブジェクトを使用します。サーバーにユーザー情報を送信する HTML ページは次のとおりです。 <HTML> <HEAD><TITLE>Dictionary Object Example</TITLE></HEAD> <BODY> Enter your name and location: <P> <FORM METHOD=POST ACTION=dictobj.asp> Your name: <INPUT TYPE="Text" NAME="VisitorName" /><P> Your location: <INPUT TYPE="Text" NAME="City" />, <INPUT TYPE="Text" NAME="State" SIZE=2 /> <P> <INPUT TYPE="Submit" VALUE="Submit" /> </FORM> <BODY> </HTML> ユーザーによって送信された情報を取り出し、エンコードし、Dictionary オブジェクトを使用して完全な状態名を取り出す ASP ページを次に示します。 <HTML> <HEAD> <TITLE>ASP Page for the Dictionary Object Example</TITLE> </HEAD> <BODY> <% Show Greeting %> <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> Sub ShowGreeting( ) Dim StateDict Dim ClientName, ClientState ' Initialize dictionary Set StateDict = Server.CreateObject("Scripting.Dictionary") StateDict.Add "NY", "New York" StateDict.Add "CA", "California" StateDict.Add "FL", "Florida" StateDict.Add "WA", "Washington" StateDict.Add "MI", "Michigan" StateDict.Add "MA", "Massachusetts" StateDict.Add "MN", "Minnesota" ' add other states ClientName = Server.HTMLEncode(Request.Form("VisitorName")) ClientState = Server.HTMLEncode(Request.Form("State")) Response.Write("Hello, " & ClientName & ". <P>") Response.Write("We are pleased to have a visitor from ") Response.Write(StateDict.Item(ClientState) & "!") End Sub </SCRIPT> </BODY> </HTML> |
|
Dictionary.Items メソッド | |
構文 | |
dictionaryobject.Items
|
|
戻り値 | |
バリアント配列。 |
|
説明 | |
指定された Dictionary オブジェクトのすべての項目が含まれた配列を返します。 |
|
一目でわかる規則 | |
返される配列は常にゼロベースのバリアント配列となり、そのデータタイプは Dictionary オブジェクトの項目に一致します。 |
|
プログラミングのヒント | |
|
|
関連項目 | |
Dictionary.Keys メソッド |
|
Dictionary.Key プロパティ | |
構文 | |
dictionaryobject.Key(key) = newkey
|
|
データタイプ | |
文字列。 |
|
説明 | |
既存のキーを新しいキーで置き換えます。 |
|
一目でわかる規則 | |
|
|
例 | |
Private Function ChangeKeyValue(sOldKey, sNewKey) 'Assumes oDictionary is a public object If oDictionary.Exists(sOldKey) Then oDictionary.Key(sOldKey) = sNewKey ChangeKeyValue = True Else ChangeKeyValue = False End If End Function |
|
プログラミングのヒント | |
|
|
Dictionary.Keys メソッド | |
構文 | |
dictionaryobject.Keys
|
|
戻り値 | |
文字列の配列 |
|
説明 | |
指定された Dictionary オブジェクトのすべての Key 値が含まれた配列を返します。 |
|
一目でわかる規則 | |
返される配列は、常に 0 ベースのバリアント配列となり、そのデータタイプは String です。 |
|
プログラミングのヒント | |
Keys メソッドは、Dictionary オブジェクトに保存されているキーのみを取り出します。Dictionary オブジェクトのすべての項目は、その Items メソッドを呼び出して取り出すことができます。Dictionary オブジェクトの Item プロパティを使用すると、個別のデータ項目を取り消すことができます。 |
|
例 | |
Dim vArray vArray = DictObj.Keys For i = 0 to DictObj.Count -1 Response.Write vArray(i) & "<BR>" Next |
|
Dictionary.Remove メソッド | |
構文 | |
dictionaryobject.Remove key
|
|
説明 | |
指定されたキーと関連付けられているデータ (項目) を辞書から削除します。 |
|
一目でわかる規則 | |
key が存在しない場合、ランタイムエラー 32811 "Element not found" が発生します。 |
|
Dictionary.RemoveAll メソッド | |
構文 | |
dictionaryobject.RemoveAll
|
|
説明 | |
辞書をクリアします。つまり、すべてのキーと関連付けられているデータを辞書から削除します。 |
|
プログラミングのヒント | |
辞書のコンテンツ全体ではなく、選択された数のメンバーを削除する場合は、Remove メソッドを使用します。 |
|