Solved

Help with vb.net dictonary

Posted on 2014-02-01
6
282 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 62

Expert Comment

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

Expert Comment

by:Miguel Oz
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now