3 Replies - 701 Views - Last Post: 06 September 2012 - 11:16 AM Rate Topic: -----

#1 shyamyn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-August 09

Using structures in a dictionary

Posted 06 September 2012 - 08:54 AM

Hi
I wanted to use structures in a dictionary so that my key can contain more values
so i did something like this

Public Structure Classes
        'Declare data members
        Public room As Integer
        Public day As Integer
        Public timeslot As Single
    End Structure
    Sub main()
        Dim room_bookings As New Dictionary(Of Classes, String)
        Dim a As New Classes()
        a.room = 1
        a.day = 10
        a.timeslot = 4
        room_bookings.Add(a, "DVB2454 ")


i had no problem doing the room booking for DVB 2454
but i am having problem printing out the oiuput

i thought this would work '

For Each pair As KeyValuePair(Of Classes, String) In room_bookings

            Console.WriteLine(pair.Key.day + pair.Key.room + pair.Key.timeslot, pair.Value)
        Next
        Console.Read()

but it doesn`t :( how am i going to do this :( help

This post has been edited by AdamSpeight2008: 01 November 2012 - 11:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using structures in a dictionary

#2 Robin19  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 206
  • View blog
  • Posts: 436
  • Joined: 07-July 10

Re: Using structures in a dictionary

Posted 06 September 2012 - 09:28 AM

I believe the key must be an Object. That is because the Dictionary uses the hash of the object. I don't believe a Structure is an Object or have a hash. Change your Classes to a Class and override the Equals and GetHashCode methods. Then it should work.
Was This Post Helpful? 0
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 227
  • View blog
  • Posts: 747
  • Joined: 19-October 11

Re: Using structures in a dictionary

Posted 06 September 2012 - 10:12 AM

What do you want to accomplish. If you want to find sum of Key values, you did it. If you want to output each value from key, and value of pair, than you should do it: Console.WriteLine((String.Format("Day: {0}, Room: {1}, Timeslot: {2}, Pair value: {3}", pair.Key.day, pair.Key.room, pair.Key.timeslot, pair.Value))).

Because now you actually have lambda expression, where you pass pair.value as object to function, that returns sum of Key.values. You should also start using & instead of + when working with strings, if that is what you wanted to do. + is fine in your case, if you wanted to get sum of values.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 958
  • View blog
  • Posts: 3,687
  • Joined: 02-July 08

Re: Using structures in a dictionary

Posted 06 September 2012 - 11:16 AM

It almost seems like your doing it backwards - Dictionary({class}, string) when maybe the string should be the key if it's a unique one. Then you add the class as the value or if you need to store many class objects to a unique identifier(the key) then Dictionary(string, List(Of {class})) would work. The way dictionarys work is by setting a unique key for each element.

This post has been edited by _HAWK_: 06 September 2012 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1