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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:06:20  来源:igfitidea点击:

Get length of a Dictionary

arraysvb.netlinqdictionary

提问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: enter image description here

我想记录 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长度,我缺少什么?

updateenter image description hereenter image description hereenter image description hereenter image description here

更新在此处输入图片说明在此处输入图片说明在此处输入图片说明在此处输入图片说明

    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..

然后打电话CountLength等等。

Also try using ForEachloop to get count of your allvariable.

还可以尝试使用ForEach循环来获取all变量的计数。