-
May 31st, 2012, 07:07 AM
#1
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.
-
May 31st, 2012, 07:21 AM
#2
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.
-
May 31st, 2012, 08:24 AM
#3
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)
{
(...........)
}
}
}
-
May 31st, 2012, 09:06 AM
#4
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
-
May 31st, 2012, 09:28 AM
#5
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
-
May 31st, 2012, 10:03 AM
#6
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)
-
May 31st, 2012, 10:21 AM
#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).
-
May 31st, 2012, 11:18 AM
#8
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.
-
May 31st, 2012, 12:03 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|