vb.net 获取字典的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13328701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get length of a Dictionary
提问by StealthRT
Hey all i am new at this Dictionary class in VB.net.
大家好,我是 VB.net 中这个 Dictionary 类的新手。
I am wanting to reteive how many items in the Dictionary array there are:

我想记录 Dictionary 数组中有多少项:

But doing this:
但是这样做:
Dim showNumber As Integer = tmpShows.Length
Does not seem to yield 4as it should?
似乎没有像它应该的那样产生4?
The code for the Dictionary i have is this:
我拥有的字典的代码是这样的:
Dim all = New Dictionary(Of String, Object)()
Dim info = New Dictionary(Of String, Object)()
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(3).ChildNodes(2).InnerText
info!Shows = From tag In .SelectNodes(".//a[@class='thickbox']")
Select New With {.Show = tag.Attributes("title").Value, .Link = tag.Attributes("href").Value}
Dim tmpShows = all.Item(info!Station)
Dim showNumber As Integer = tmpShows.Length
What am i missing in order to get the 4length i am looking for?
为了获得我正在寻找的4长度,我缺少什么?
update



更新



Dim all = New Dictionary(Of String, Object)()
For Each channel In doc.DocumentNode.SelectNodes(".//div[@class='channel_row']")
Dim info = New Dictionary(Of String, Object)()
skipFirstShow = False
With channel
info!Logo = .SelectSingleNode(".//img").Attributes("src").Value
info!Channel = .SelectSingleNode(".//span[@class='channel']").ChildNodes(3).ChildNodes(0).InnerText
info!Station = .SelectSingleNode(".//span[@class='channel']").ChildNodes(3).ChildNodes(2).InnerText
Dim style As String = .SelectSingleNode(".//span[2]").Attributes("style").Value
If InStr(style.ToLower, "width: 0px;") <> 0 Then skipFirstShow = True
info!Shows = From tag In .SelectNodes(".//a[@class='thickbox']")
Select New With {.Show = tag.Attributes("title").Value, .Link = tag.Attributes("href").Value}
'Select New With {.Show = tag.Attributes("title").Value, .Link = tag.Attributes("href").Value}
End With
all.Add(info!Station, info.Item("Shows"))
theLogoURL(theCount) = "https://xxx.com" & Trim(info.Item("Logo"))
theChannelNum(theCount) = Trim(info.Item("Channel"))
theStationCallLetters(theCount) = Trim(info.Item("Station"))
Dim Shows As String = ""
Dim ShowsDetail As String = ""
Dim tmpShows = all.Item(info!Station)
回答by Kundan Singh Chouhan
Use Count instead of length.
使用计数而不是长度。
Example :
例子 :
Dim showNumber As Integer = tmpShows.Count
回答by StealthRT
I just did this to get my count...
我这样做只是为了算数……
Dim intXShows As Integer = 0
For Each item In tmpShows
intXShows += 1
Next
回答by NeverHopeless
Have you tried to change your declaration like this:
您是否尝试过像这样更改您的声明:
Dim all as New Dictionary(Of String, Object)()
instead of:
代替:
Dim all = New Dictionary(Of String, Object)()
and then call Count, Lengthetc..
然后打电话Count,Length等等。
Also try using ForEachloop to get count of your allvariable.
还可以尝试使用ForEach循环来获取all变量的计数。

