NotABlueWhale
NotABlueWhale

Reputation: 795

DateTime as Object in dictionary

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

Answers (2)

Steven Doggart
Steven Doggart

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

David Sdot
David Sdot

Reputation: 2333

As you have a list of Dictonary "endtime" is here: lobjWaveOutList(1)("endTime")

Upvotes: 0

Related Questions