1

Hi I have this dictionary..

Dim Rooms As New Dictionary(Of Integer, Of Integer)

Rooms(1) = 101, 102, 109, 110
Rooms(2) = 103, 104, 105
Rooms(3) = 106, 107

I want to know if i can obtain how many keys are in the dictionary

For example, in this dictionary i have 3 keys, and if i use Rooms.Count it returns me 9 thats each pair of keys-values, but i want to obtain 3, each diferent value as key.

EDIT: Sintax error P.D: I cant use now because i have to use in a webservice filtered by IP, but VS2010 if i use count on the dictionary, tells me that will contain the number of the keys/value pairs.

bombai
  • 927
  • 1
  • 17
  • 27
  • 1
    Dictionary keys must be unique, I would expect this to fail at runtime. I don't think a Dictionary is the right thing for your needs. – vcsjones Jun 09 '12 at 15:02
  • 1
    In your last example you have only 3 keys, not 9. And you can't use that syntax to set the values. Each key in a Dictionary(Of Integer, Integer) has only one value not 4,3,2 – Steve Jun 09 '12 at 15:13
  • Yes is for a more complex sintax, but i have used this one like a simple example, i have to use one dictionary of string, of dictionary (of integer, of a list) – bombai Jun 09 '12 at 15:15

3 Answers3

3

If you are talking about System.Collections.Generic.Dictionary(Of TKey, Of TValue), your code will throw an ArgumentException because you are adding the same key several times.

It still will not work if you use the indexer instead:

Rooms(1) = 101
Rooms(1) = 102
...

Like this, there will be no exception, but the second line will just overwrite the value stored in the first line.

It seems that you actually want to use a Dictionary(Of Integer, Of List(Of Integer)). Use it like this:

Dim Rooms = Rooms As Dictionary(Of Integer, Of List(Of Integer))

Rooms(1) = New List(Of Integer) From { 101, 102, 109, 110 }
Rooms(2) = New List(Of Integer) From { 103, 104, 105 }
Rooms(3) = New List(Of Integer) From { 106, 107 }

Rooms.Count will then provide you with the number of keys in the dictionary.

If, and only if, your lists for each key do not change in length later on, you can use arrays instead of List(Of Integer) instances.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
  • Sorry, i have written thinking as a list. In this case is like this Rooms(1) = 101, 102, 109, 110 Rooms(2) = 103, 104, 105 Rooms(3) = 106, 107 How can i know what is the number of the keys? 3 in this case.. Thanks and sorry. – bombai Jun 09 '12 at 15:06
  • If you use a `Dictionary(Of Integer, Of List(Of Integer))`, the `Count` property of the dictionary will in fact return the number of keys that you're looking for. – O. R. Mapper Jun 09 '12 at 15:09
  • Sure? VS2010 tell me that is the number of pairs, not the number of diferent keys, and i cant test it now..Thanks. – bombai Jun 09 '12 at 15:25
  • 1
    In a `Dictionary(Of Integer, Of List(Of Integer))`, you have pairs of two values, one being the `Integer`, the other one being the `List(Of Integer)`. There is one such pair for each added key, and each key can be added at most once. Hence, `Count` is the number of keys. – O. R. Mapper Jun 09 '12 at 15:39
1

if you add the same key into the dictionary, you will get an exception. if this was a list of keyvaluepairs, you can use the groupby linq operator

    Dim rooms = New List(Of KeyValuePair(Of String, String))()

rooms.Add(New KeyValuePair(Of String, String)("one", "thing1"))
rooms.Add(New KeyValuePair(Of String, String)("one", "thing2"))
rooms.Add(New KeyValuePair(Of String, String)("two", "other1"))
rooms.Add(New KeyValuePair(Of String, String)("two", "other2"))

Dim query = rooms.GroupBy(Function(x) x.Key)
For Each g As var In query
    Console.WriteLine("Key: " & Convert.ToString(g.Key))
    For Each v As var In g
        Console.WriteLine(vbTab & Convert.ToString(v.Value))
    Next
Next
AaronHS
  • 1,334
  • 12
  • 29
0

Se my answer to SO question c# dictionary How to add multiple values for single key? where I show my implementation of a multi map. This is basically a dictionary with list entries. It allows you to store a list of values for each key.

Community
  • 1
  • 1
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188