2012-11-11 137 views
1

嘿,我在VB.net的這個Dictionary類中新增了這個新功能。獲取字典的長度

我想reteive多少個項目的字典數組中有: enter image description here

但是這樣做:

Dim showNumber As Integer = tmpShows.Length 

似乎

不產生,因爲它應該?

的解釋我的代碼是這樣的:

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 

我缺少的是爲了得到長我找的?

更新 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://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) 

回答

6

使用次數,而不是長度。

例子:

Dim showNumber As Integer = tmpShows.Count 
+0

它以** 0 **的形式出現? – StealthRT

+0

@StealthRT,試試這個** tmpShows.ToList()。Count ** –

+0

然後你正在做別的事情是錯的 – codingbiz

0

我只是這樣做是爲了讓我的個性化......

Dim intXShows As Integer = 0 

For Each item In tmpShows 
    intXShows += 1 
Next 
0

您是否嘗試過改變你的聲明是這樣的:

Dim all as New Dictionary(Of String, Object)() 

代替:

Dim all = New Dictionary(Of String, Object)() 

,然後調用CountLength等。

同時嘗試使用ForEach循環,讓您的all變量的數量。