字典中的VB.NET获取密钥分组 [英] Dictionary in VB.NET obtain keys grouped

查看:295
本文介绍了字典中的VB.NET获取密钥分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有这个字典..

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

例如,在这个字典中,我有3个键,如果我使用Rooms.Count它返回我每个键值对9,但我想获得3,每个不同的值作为键。

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.

编辑:Sintax错误
PD:我现在不能使用,因为我必须使用通过IP过滤的Web服务,但VS2010如果我使用计数字典,告诉我将包含键/值对的编号。

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.

推荐答案

正在谈论 System.Collections.Generic.Dictionary(Of TKey,Of TValue),你的代码会抛出 ArgumentException

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.

看起来你真的想使用 Dictionary(Of Integer,Of List(Of Integer))。使用它像这样:

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 会为您提供字典中的键数。

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

如果,对于每个键稍后不改变长度,可以使用数组而不是 List(Of Integer)实例。

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.

这篇关于字典中的VB.NET获取密钥分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆