将项添加到词典

时间:2015-12-01 18:02:18

标签: vb.net

如果找到密钥,如何将值添加到字典中的字符串列表中。如果找到密钥,则无法添加到字典

   Dim DataDictionary As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))

     DataDictionary.Add("1", SAM, BOB)
'if key is found add Joe
   If DataDictionary .ContainsKey("1")Then
    'Add Joe 

1 个答案:

答案 0 :(得分:2)

Dim DataDictionary As New Dictionary(Of String, List(Of String))
' ...
' Initialize dictionary with a list
DataDictionary.Add("1", New List(Of String)(New String() { "SAM", "BOB" }))

' ...
' Check for key and add another value.
If DataDictionary.ContainsKey("1") Then
  'Add to the list of strings
  DataDictionary("1").Add("new item")
End If