Solved

Help with vb.net dictonary

Posted on 2014-02-01
6
285 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

939 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

10 Experts available now in Live!

Get 1:1 Help Now