In a List with Dictionaries, I am unable to show the name of the Dictionary. How can I get that done? In For Each Item, Item already gets the String, String. See image, it says: The value of type System.Collections.Generic.Dictionary(Of String, String) cannot be converted to String.
How?
Dim ListOfDictionaries = New List(Of Dictionary(Of String, String)) ' From { Dictionary1, Dictionary2 }
Dim Dictionary1 As New Dictionary( Of String, String)
Dim Dictionary2 As New Dictionary( Of String, String)
Dim Dictionary3 As New Dictionary( Of String, String)
ListOfDictionaries.Add(Dictionary1)
ListOfDictionaries.Add(Dictionary2)
ListOfDictionaries.Add(Dictionary3)
Logger.Info(ListOfDictionaries.Count)
If Not ListOfDictionaries.Contains(Dictionary3) Then
ListOfDictionaries.Add(Dictionary3)
End If
Dictionary1.Add("A", "Letter")
Dictionary1.Add("B", "Letter")
Dictionary1.Add("C", "Letter")
Dictionary2.Add("A", "Letter")
Dictionary2.Add("B", "Letter")
Dictionary2.Add("C", "Letter")
Dictionary3.Add("A", "Letter")
Dictionary3.Add("B", "Letter")
Dictionary3.Add("C", "Letter")
Logger.Info(ListOfDictionaries.Count)
For Each item In ListOfDictionaries
Logger.Info(Item)
For Each pair As KeyValuePair(Of String, String) In item
Logger.Info(pair.Key,pair.Value)
Next
Next
CodePudding user response:
Instead of List(Of Dictionary(Of String, String))
you could have Dictionary(Of String, Dictionary(Of String, String))
. Then you can do dictionaryOfDictionaries.Add("Dictionary1", dictionary1)
, like this:
Dim allDictionaries = New Dictionary(Of String, Dictionary(Of String, String))
Dim dictionary1 As New Dictionary(Of String, String)
Dim dictionary2 As New Dictionary(Of String, String)
Dim dictionary3 As New Dictionary(Of String, String)
allDictionaries.Add("Dictionary1", dictionary1)
allDictionaries.Add("Dictionary2", dictionary2)
allDictionaries.Add("Dictionary3", dictionary3)
Logger.Info(allDictionaries.Count)
If Not allDictionaries.ContainsKey("Dictionary3") Then
allDictionaries.Add("Dictionary3", dictionary3)
End If
dictionary1.Add("A", "Letter")
dictionary1.Add("B", "Letter")
dictionary1.Add("C", "Letter")
dictionary2.Add("A", "Letter")
dictionary2.Add("B", "Letter")
dictionary2.Add("C", "Letter")
dictionary3.Add("A", "Letter")
dictionary3.Add("B", "Letter")
dictionary3.Add("C", "Letter")
Logger.Info(allDictionaries.Count)
For Each item In allDictionaries
Logger.Info(item.Key)
For Each pair As KeyValuePair(Of String, String) In item.Value
Logger.Info(pair.Key, pair.Value)
Next
Next
CodePudding user response:
A dictionary doesn't have a name. It's just a data structure with some contents. If you mean you want to log Dictionary1/Dictionary2/Dictionary3 from the loop in the question, then you can't. These are just temporary variable names that are being used to set up the ListOfDictionaries. These names are not stored in ListOfDictionaries.
One approach to this is to add a Name property to your Dictionary yourself and store your dictionary name in it:
Dim ListOfDictionaries = New List(Of Dictionary(Of String, String)) ' From { Dictionary1, Dictionary2 }
Dim Dictionary1 As New Dictionary(Of String, String)
Dim Dictionary2 As New Dictionary(Of String, String)
Dim Dictionary3 As New Dictionary(Of String, String)
ListOfDictionaries.Add(Dictionary1)
ListOfDictionaries.Add(Dictionary2)
ListOfDictionaries.Add(Dictionary3)
Logger.Info(ListOfDictionaries.Count)
If Not ListOfDictionaries.Contains(Dictionary3) Then
ListOfDictionaries.Add(Dictionary3)
End If
Dictionary1.Add("Name", "Dictionary1")
Dictionary1.Add("A", "Letter")
Dictionary1.Add("B", "Letter")
Dictionary1.Add("C", "Letter")
Dictionary2.Add("Name", "Dictionary2")
Dictionary2.Add("A", "Letter")
Dictionary2.Add("B", "Letter")
Dictionary2.Add("C", "Letter")
Dictionary3.Add("Name", "Dictionary3")
Dictionary3.Add("A", "Letter")
Dictionary3.Add("B", "Letter")
Dictionary3.Add("C", "Letter")
Logger.Info(ListOfDictionaries.Count)
For Each item In ListOfDictionaries
Logger.Info(item.Item("Name"))
For Each pair As KeyValuePair(Of String, String) In item
If pair.Key <> "Name" Then
Logger.Info(pair.Key & ", " & pair.Value)
End If
Next
Next
Output:
3
3
Dictionary1
A, Letter
B, Letter
C, Letter
Dictionary2
A, Letter
B, Letter
C, Letter
Dictionary3
A, Letter
B, Letter
C, Letter