Solved

Help with vb.net dictonary

Posted on 2014-02-01
6
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 63

Expert Comment

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

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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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 36

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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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