2012-02-22 24 views
1

在字典中的值我有這個dictoinary:如何找到在VB

Dim chardict As Dictionary(Of Char, Integer) = Nothing 
chardict.Add("A", 0) 
chardict.Add("B", 1) 

我想如果語句,但我被困在語法有點做到以下幾點:

if chardict.containskey("A") 
    'display the value that corrosponds to the letter "A" 
    'in this case display the character 0 
    [some code] 
end if 

謝謝推進

+0

假設你實際上在某個地方創建了chardict並且禁用了option-strict以允許嚴格g-> char(將字符串不是更好的選擇?),應該工作,您需要擴展您的示例 – 2012-02-22 10:43:11

回答

-1

你的語法似乎是正確的

If dictionary.ContainsKey("A") Then 
    Do 
End If 

你得到了什麼錯誤?

+0

哦,我希望它顯示對應於字母A的值。 在這種情況下顯示字符0 – 2012-02-22 10:46:21

+1

如果內部環,添加, 昏暗NUM爲整數= dictionary.Item(「A」) Console.WriteLine(NUM) – 2012-02-22 10:53:29

+0

我發現了一個錯誤「的NullReferenceException是未處理的」未設置爲實例 對象引用一個對象(這是指字典'chardict.Add(「A」,0) – 2012-02-22 11:09:30

-1

只是傳遞價值,詞典,如果字典是如下它會回報你的關鍵手段:

Private Status As New Dictionary (Of String, String) 
Status.Add("Y", "Married") 
Status.Add("N", "Single") 
Status.Add("X", "Its Complicated") 

下面的一行代碼將返回鍵

Dim Key As String = Status("Single") 

關鍵= N

+0

你測試了你的代碼嗎? – LarsTech 2017-05-26 14:39:21