Reputation: 795
I have read here that a dictionary (of string, object) can hold multiple variable types. However, in the function below, endTime does not appear to get assigned. The line Console.Write(lobjWaveOutList(0)("endTime"))
in the code below gives me the error 'the given key was not present in the dictionary'.
Private lobjWaveOutList As New List(Of Dictionary(Of String, Object))()
Public Sub addIndex(waveOut As Object, endTime As DateTime)
Console.WriteLine("endTime:")
Console.WriteLine(endTime)
lobjWaveOutList.Add(New Dictionary(Of String, Object)() From {{"waveOut", waveOut}})
lobjWaveOutList.Add(New Dictionary(Of String, Object)() From {{"endTime", endTime}})
Console.Write(lobjWaveOutList(0)("endTime"))
End Sub
I called the addIndex function with the following parameters:
waveouts.addIndex(New WaveOut(), DateTime.Now.AddSeconds(10))
Upvotes: 2
Views: 291
Reputation: 43743
lobjWaveOutList(0)("endTime")
will not work because it is accessing the first dictionary in the list, which only contains a "waveOut"
item. The "endTime"
item is in the second dictionary in the list. To get that one, you'd need to do this:
Console.Write(lobjWaveOutList(1)("endTime"))
As Neolisk pointed out, it would seem more appropriate, based on your example, to simply have a single dictionary, containing multiple items, rather than a list of dictionaries, each only containing a single item.
Upvotes: 2
Reputation: 2333
As you have a list of Dictonary "endtime" is here: lobjWaveOutList(1)("endTime")
Upvotes: 0