Solved

Help with vb.net dictonary

Posted on 2014-02-01
6
300 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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