Get the length of a Dictionary in VB.NET

10,809

Solution 1

I just did this to get my count...

 Dim intXShows As Integer = 0

 For Each item In tmpShows
    intXShows += 1
 Next

Solution 2

Use Count instead of length.

Example:

Dim showNumber As Integer = tmpShows.Count
Share:
10,809

Related videos on Youtube

StealthRT
Author by

StealthRT

Updated on July 11, 2022

Comments

  • StealthRT
    StealthRT almost 2 years

    I want to retrieve how many items there are in the Dictionary array:

    Enter image description here

    But doing the following does not seem to yield 4 as it should:

    Dim showNumber As Integer = tmpShows.Length
    

    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 4 length I am looking for?

    Enter image description here

    Enter image description here

    Enter image description here

    Enter 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://example.org" & 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)
    
  • Kundan Singh Chouhan
    Kundan Singh Chouhan over 11 years
    @StealthRT, try this tmpShows.ToList().Count
  • StealthRT
    StealthRT over 11 years
    @KundanSinghChouhan Still a 0 :o/
  • Kundan Singh Chouhan
    Kundan Singh Chouhan over 11 years
    @StealthRT, seems you are doing something wrong in your code. What type of data you are getting when you see that in quick watch?
  • StealthRT
    StealthRT over 11 years
    @KundanSinghChouhan Updated my OP with the image of the quick watch.
  • Kundan Singh Chouhan
    Kundan Singh Chouhan over 11 years
    @StealthRT, also post the full quick watch view of tmpShows
  • StealthRT
    StealthRT over 11 years
    @KundanSinghChouhan Updated again with that request.
  • Kundan Singh Chouhan
    Kundan Singh Chouhan over 11 years
    @StealthRT, as you can see the tmpShows is a null object,just pass that condition in debugger and than take a screenshot of quick watch. Right now the code doesn't executed yet.
  • codingbiz
    codingbiz over 11 years
    did you try the .ToList().Count?
  • Kundan Singh Chouhan
    Kundan Singh Chouhan over 11 years
    @StealthRT, can you also post tmpShows.ToList() with quick watch?
  • codingbiz
    codingbiz over 11 years
    and what namespace are you using, I don't have that special .Item on my dictionary
  • StealthRT
    StealthRT over 11 years
    @KundanSinghChouhan Done. Updated OP.
  • Victor Zakharov
    Victor Zakharov over 11 years
    @StealthRT: Count is the correct property.
  • Victor Zakharov
    Victor Zakharov over 11 years
    @StealthRT: Count always works. Try reducing your example to something that is self-contained and as a result can be reproduced standalone.
  • Peter Mortensen
    Peter Mortensen almost 3 years
    Couldn't something be said about the efficiency?