密鑰必須是一個字典是獨一無二的。所以,如果你想創建一組獨特的值,將它們分配爲字典中的鍵。基本上,在你的方案中,只需交換密鑰名稱和值。
Dim d
Set d = CreateObject("Scripting.Dictionary")
If Not d.Exists("JAVA") Then d.Add "JAVA", "one" ' Will get added
If Not d.Exists("NET") Then d.Add "NET", "two" ' Will get added
If Not d.Exists("C") Then d.Add "C", "three" ' Will get added
If Not d.Exists("C++") Then d.Add "C++", "four" ' Will get added
If Not d.Exists("JAVA") Then d.Add "JAVA", "five" ' Will NOT get added.
If Not d.Exists("C") Then d.Add "C", "six" ' Will NOT get added.
If Not d.Exists("C++") Then d.Add "C++", "sev" ' Will NOT get added.
現在,您可以測試以查看該密鑰是否已存在於字典中,並且只有在該字典不存在的情況下才添加該密鑰。不可能多次添加"JAVA"
(或任何鍵)。
您沒有重複。您不能將重複項添加到字典中。編輯:哎呀,沒有意識到你的意思。就像@Bond所說的,只需添加鑰匙並捕捉失敗。 (可能是在錯誤恢復下一個) – Adam47
@ Adam47你可以有重複的_values_,只是不重複的_keys_,這引發了問題......爲什麼不把它們創建爲_keys_,然後他們保證是唯一的。 – Bond
JAVA,C,C++是重複的值,對吧? – knvchowdary