CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2012
    Posts
    7

    ArrayList adding item

    Hi everyone,

    I'm new to c#, learning and coding.
    That's the my problem;

    Code:
    namespace Google
    {
        public partial class Form1 : Form
        {
            private string url;
            private string geturl_2;
            private string geturl_els;
            private int numbers;
            
    
            
            public Form1()
            {
                InitializeComponent();
               CenterToScreen();
            }
    
    
    (............)
    
    
    private void search_Click(object sender, EventArgs e)
            {
    if (numbers <= 10)
                {
     (........)
                 url = "https://www.google.com.tr/search?q=" + searchtext;
     (........)
    
                    Thread thread1 = new Thread(new ThreadStart(googlesearch));
                    thread1.Start();
    
    
                }
    }
    
    (..................)
    
    
    
       private void googlesearch()
            {
    
                
                ArrayList urlpool0 = null;
                HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(@url);
                HtmlAgilityPack.HtmlNodeCollection rowNodes = doc.DocumentNode.SelectNodes("//cite");
                foreach (HtmlAgilityPack.HtmlNode node in rowNodes)
                {
                    string url_1 = node.InnerText;
                    urlpool0 = new ArrayList();
                    urlpool0.Add(url_1);              // <- This is the my problem.I want to add the elements to array but only one element adding
                    //textBox4.AppendText(url_1 + "\r\n"); // <- No problem on debuging code, 10 url adding to listview
                    textBox4.AppendText(urlpool0.Count + "\r\n"); //<-Debuging. Actually should be added to the textbox (status textbox and multiline) like this "1,2,3.. " but adding text to textbox "1,1,1,1,1,1,1...."
                }
    
                Fin(urlpool0);
            }
    
    
       private void fin(ArrayList arraytest)
            {
                foreach (string geturladdr in arraytest)
                {
                   (...........)
                }
    
            
            }
    
    
    }

    Regards,
    Thank you.

  2. #2
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    I learned now; only one element at the same time add the element with "Add" function.OK so how do i do this? append element to array in foreach.

  3. #3
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    Ok I did, here is my solution (strong code? or good idea?)



    Code:
    namespace Google
    {
        public partial class Form1 : Form
        {
            private string url;
            private string geturl_2;
            private string geturl_els;
            private int numbers;
            
    
            
            public Form1()
            {
                InitializeComponent();
               CenterToScreen();
            }
    
    
    (............)
    
    
    private void search_Click(object sender, EventArgs e)
            {
    if (numbers <= 10)
                {
     (........)
                 url = "https://www.google.com.tr/search?q=" + searchtext;
     (........)
    
                    Thread thread1 = new Thread(new ThreadStart(googlesearch));
                    thread1.Start();
    
    
                }
    }
    
    (..................)
    
    
    
       private void googlesearch()
            {
    
                
                //ArrayList urlpool0 = null;
                HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(@url);
                HtmlAgilityPack.HtmlNodeCollection rowNodes = doc.DocumentNode.SelectNodes("//cite");
    
                int count = 0;
                foreach (HtmlAgilityPack.HtmlNode node in rowNodes){ count++; } // getting result count 
                string[] numss = new string[count];   // creating string array and setting count/buffer
    
                int i = 0; // integer i, volue is 0
                foreach (HtmlAgilityPack.HtmlNode node in rowNodes)
                {
                    string url_1 = node.InnerText;
    
                    numss[i] = url_1;  // inserting results to numss array with "i" variable
                    //urlpool0 = new ArrayList();
                    //urlpool0.Add(url_1);
                    //textBox4.AppendText(url_1 + "\r\n");
                   // textBox4.AppendText(urlpool0.Count + "\r\n");
                    i++; // increase in  the i variable
                }
    
                fin(numss);
            }
    
    
       //private void fin(ArrayList arraytest) replaced with
         private void fin(string[] arraytest)
            {
                foreach (string geturladdr in arraytest)
                {
                   (...........)
                }
    
            
            }
    
    
    }

  4. #4
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    The problem continues, this time


    Code:
    (.............)
    
     private void googlesearch()
            {
                int whilenum;
                string[] numss = null;
                int count8 = 0;
                for (whilenum = 0; whilenum <= numbers; whilenum += 10) // google result pages 1 to 2 etc... Logic error in "count8" variable
                {
                    
                    string geturl_3 = geturl_2 + whilenum;
                    HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
                    HtmlAgilityPack.HtmlDocument doc = web.Load(@geturl_3);
                    HtmlAgilityPack.HtmlNodeCollection rowNodes_2 = doc.DocumentNode.SelectNodes("//cite");
    
    
                     foreach (HtmlAgilityPack.HtmlNode node in rowNodes_2) { textBox4.AppendText(count8 + "\r\n"); count8++; }
                    //textBox4.AppendText(count8 + "\r\n");
                    numss = new string[count]; // logic error here.The second time turn "numss" array variable returned re-programmed.
    
                    int i = 0;
                    foreach (HtmlAgilityPack.HtmlNode node_2 in rowNodes_2)
                    {
                        string url_2 = node_2.InnerText;
                        //textBox4.AppendText(url_2 + "\r\n");
                        numss[i] = url_2;
                        i++;
                    }
                }
               fin(numss);
            
            }
    
    
    (...........)
    There is a logic error here.How to set automatically array buffer? Or is there another way of writing?

    Thanks

  5. #5
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: ArrayList adding item

    I would go for this
    Code:
            private void googlesearch()
            {
                int whilenum;
                List<string> numss = new List<string>();
                int count8 = 0;
                for (whilenum = 0; whilenum <= numbers; whilenum += 10) // google result pages 1 to 2 etc... Logic error in "count8" variable
                {
    
                    string geturl_3 = geturl_2 + whilenum;
                    HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
                    HtmlAgilityPack.HtmlDocument doc = web.Load(@geturl_3);
                    HtmlAgilityPack.HtmlNodeCollection rowNodes_2 = doc.DocumentNode.SelectNodes("//cite");
    
    
                    foreach (HtmlAgilityPack.HtmlNode node in rowNodes_2) { textBox4.AppendText(count8 + "\r\n"); count8++; }
                    //textBox4.AppendText(count8 + "\r\n");
                    numss.Clear();    
                    //I repeat the idea you are using the "new" keyword which clear each loop the contents of numss. Is it normal ? 
                    int k;
                    for (k = 0; k < count; k++) { numss.Add(string.Empty); }
    
                    int i = 0;
                    foreach (HtmlAgilityPack.HtmlNode node_2 in rowNodes_2)
                    {
                        string url_2 = node_2.InnerText;
                        //textBox4.AppendText(url_2 + "\r\n");
                        numss[i] = url_2;
                        i++;
                    }
                }
                fin(numss.ToArray());
    
            }
    Last edited by Erendar; May 31st, 2012 at 09:32 AM. Reason: questionning

  6. #6
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    Thank you erendar but this is not solution. I want this; (example) 2 google result page parsing and all urls in 2 pages add a ArrayList. I produced 2 solution for this, 1-ArrayList class and List<string> class.Problem in here (your code and my code)


    for( getting google 2 results for parse with net){

    for(parsing google results urls){

    #1 running
    and adding array to 1 page results - examples of string memory buffer 10 (because 10 results of 1 page)

    }

    #2 running (that's the problem)
    array reprogramming and memmory buffer change to 20 (results of page 1 to 2 so, 10+10 = 20 string[20] )

    your code numss.Add(string.Empty); empty reprogramming numss array and results not displaying.Like this;

    Array reflection in listview after go to "fin()"


    10 real result
    xxx.com
    xxxe.com
    xxxv.com (after displaying "space" because array reprogramming returning 2th for page result 2)
    (space)
    (space)
    (space)

    }

    Thank you
    (sorry my for bad english)

  7. #7
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    ArrayList class, Add method adding 1 item running in foreach.What can I use "array" type in foreach or array algorithm (just an example).

  8. #8
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: ArrayList adding item

    To fix your original problem :

    Code:
    foreach (HtmlAgilityPack.HtmlNode node in rowNodes)
    {
        string url_1 = node.InnerText;
        urlpool0 = new ArrayList(); // problem is here ! creating a new list every time around the loop
        urlpool0.Add(url_1);              
    }
    You're creating a new instance of the array list for each iteration through the loop.

    Do this instead :

    Code:
    urlpool0 = new ArrayList();
    foreach (HtmlAgilityPack.HtmlNode node in rowNodes)
    {
        string url_1 = node.InnerText;
        urlpool0.Add(url_1);              
    }
    As a note you should be using List<string> instead of ArrayList. This is the recommended dynamically sizing list class as of .NET 2.0. ArrayList is a 1.1 collections object.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  9. #9
    Join Date
    May 2012
    Posts
    7

    Re: ArrayList adding item

    Thank you darwen, i'm so stupid :/ this is very easy problem but my brain so tired. Thank you again.And you erendar, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured