Solved

Help with vb.net dictonary

Posted on 2014-02-01
6
292 Views
Last Modified: 2014-02-10
Hi guys..... im looking my dictionary and im trying to search to see if the key is within the dictionary - if so i just need to update the value and if not add the key and value to the dictionary ----so i did this:

   If Dictionary.ContainsKey(thescorestext) Then

                    Dictionary(thescorestext) = Dictionary(thescorestext) + div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2)
                Else
                    Dictionary.Add(thescorestext, div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2))
                End If

Open in new window



but when i check it after i get 0 meaning nothing has been inserted into the dictionary...am i doing something wrong?

using  scorestext.Text = "The count is:" & dictionary.Count for out put of 0
0
Comment
Question by:runnerjp2005
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39826807
The code look OK. Are you getting any exceptions?
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39826811
Code seems OK, but I need more info:
Q1. Is the dictionary key value of type string, string?
Check how you create Dictionary instance as follows:
Dim Dictionary As New Dictionary(Of String, String)
before using your posted code.
Q2.What are the type and possible values of thescorestext?
Q3. Any exceptions being reported?

Further examples on how to use Dictionary are shown here:
http://www.dotnetperls.com/dictionary-vbnet
0
 

Author Comment

by:runnerjp2005
ID: 39826845
i have followed the examples to get this far :)

outout of thescorestext is something like:

47 Galway United 4 - 1 Sligo Rovers 2/1/2014 1:31:43 PMBoom!


no exceptions been reported at all

and its set as    Dim dictionary As New Dictionary(Of String, String)
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39828710
Since Dictionary is a class name, I would suggest not using that for your variable name. That can be very confusing when looking at the code.

Other than that, I agree with the above comments:  Your code looks fine.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39831053
Could you replace
Dictionary.Add(thescorestext, div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2))
 
with
dictionary.Add(thescorestext, "test me")

and check that test me is stored  in the dictionary instance and count is 1.

I have tested the replacement code and it works OK.
Possible issues:
1) Check how dictionariy is used and instantiated inside your implementation, you may be clearing the dictionary or creating a new instance between adding the new key and checking the count.
2) div.selectSingleNode(".//td[@class='time']").InnerText.Trim().Substring(0, 2) code is not working properly thus the add is not working. Store it in a variable and check that the variable has a valid string that is added to dictionary instance .
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39831085
Hi runnerjp2005;

I suspect that the reason you are always seeing 2 entries in the dictionary and that "DOT4" is never there is that each time the page loads it has been reinitializing the dictionary to no entry and in page load putting the 2 entries back in. You need to check if it is a post back and if it is not initialize the  dictionary and if it is a post back refill the dictionary with previous values. As seen below. You also need to save the dictionary before PreRender to have the values available to be reloaded into the dictionary. Also if you attempt to load an entry into the dictionary with a key that already exist it will throw an exception.

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    ' In the Page_Load event you need to check if it is a post back. 
    ' If it is an initial load then add the two dictionary values other 
    ' wise restore the values in the dictionary with its previous values. 
    If Not Page.IsPostBack Then
        ' Initial load of dictionary
        dictionary.Add("Dot2", "Dot2")
        dictionary.Add("Dot3", "Dot3")
    Else
        ' Get the previous values of the dictionary and restore it to the variable
        dictionary = CType(Me.ViewState("ScoreDictionary"), Dictionary(Of String, String))
    End If

End Sub



Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
    ' Save dictionary before the page is rendered.
    ' to be re-loaded on the next page request
    Me.ViewState.Add("ScoreDictionary", dictionary)
End Sub

Function setscores() As String

     Dim thescorestext As String
     Dim webGet As New HtmlWeb() 'open the system
     Dim htmlDoc As HtmlDocument = webGet.Load("http://www.livefutbol24.com/Football/Live") '' get the html from the webpage

     For Each div As Object In htmlDoc.DocumentNode.SelectNodes(".//div[@class='matches']") ' select all the divs within the code that contain *

         If Not div.selectSingleNode(".//td[@class='score']") Is Nothing Then

             ' You other code here .....
             
             ' Because this is the same value being loaded each time this function
             ' is called it will throw an exception if it is already there. So check to see if it is 
             ' already there before adding.
             If Not dictionary.ContainsKey("Dot4") Then
                 dictionary.Add("Dot4", "Dot4")
             End If

         End If

     Next

     Return thescorestext

End Function    

Open in new window

0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question