1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Array keeps going out of bounds

Discussion in 'Visual Basic .NET' started by Kalashnikov, May 22, 2013.

  1. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    Array defined as
    Code:
    Public scraped_users_array(25000) As String '25k max
    Code:
        Private Function ScrapeUsers()        Dim browser_text As String = browser.DocumentText
            Dim allelements As HtmlElementCollection = browser.Document.All
            For Each webpageelement As HtmlElement In allelements
                Dim raw_id = webpageelement.GetAttribute("href")
                If raw_id <> "" & raw_id.Contains("/user/") Then
                    Dim new_id As String = Replace(raw_id, "http://www.youtube.com/user/", "")
                    scraped_users_array(current_user_scraped_id) = new_id
                    current_user_scraped_id = current_user_scraped_id + 1
                End If
            Next
            scraped_users_array = RemoveDups(scraped_users_array, True)
            Dim scraped_count As Integer = 0
            For Each element As String In scraped_users_array
                If element <> "" Then
                    txtScrapedList.Items.Add(element)
                    scraped_count = scraped_count + 1
                End If
            Next
            current_page = current_page + 1
            scraping = True
            browser.Navigate("http://www.youtube.com/all_comments?v=" + txtVideoID.Text + "&page=" + current_page.ToString)
            MessageBox.Show("Scraped " + scraped_count.ToString() + " users.")
            Return True
        End Function
    Gets to page 2 and then this happens
    [​IMG]

    Really confusing me as I can't see the flaw in this? Code is fairly simple and the if statements should prevent it from EVER exceeding 25k.
     
    Last edited: May 22, 2013
  2. thejake

    thejake Jr. VIP Jr. VIP Premium Member

    Joined:
    Nov 13, 2009
    Messages:
    685
    Likes Received:
    828
    current_user_scraped_id isn't initialized to something sensible
     
  3. Kalashnikov

    Kalashnikov Junior Member

    Joined:
    Feb 18, 2011
    Messages:
    148
    Likes Received:
    59
    Location:
    Northern Ireland
    But if I messagebox current_user_scraped_id within the if statements it loops through the entire array when it shouldn't because of the if statements
     
  4. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    Aaa, why don't you use a List ?
    You can use HashSet too, it will remove duplicates automatically.
     
    • Thanks Thanks x 1