Display ArrayList in ListBox
-
On a windows for, after clicking a button, I add a class Student to an ArrayList with ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(a, b, c, d, e); //Student declared as public class elsewhere This seem to work fine. However, I then want to load the items in a Listbox when another button is clicked. I use foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); I get message that Grade10 does not exist in this context. If I declare Grade10 again in this event it appears that the ArrayList is empty - program runs, no errors, empty ListBox. All samples I find refer to console apps. These work for me but I cannot sort out the windows form - my first attempt at C#, I've been using Delphi for ages. Seems I have to declare ArrayList Grade10 somewhere else (global?) so it is visible inside the second button event as well? Much appreciate someone showing me the way. Hannes
-
On a windows for, after clicking a button, I add a class Student to an ArrayList with ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(a, b, c, d, e); //Student declared as public class elsewhere This seem to work fine. However, I then want to load the items in a Listbox when another button is clicked. I use foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); I get message that Grade10 does not exist in this context. If I declare Grade10 again in this event it appears that the ArrayList is empty - program runs, no errors, empty ListBox. All samples I find refer to console apps. These work for me but I cannot sort out the windows form - my first attempt at C#, I've been using Delphi for ages. Seems I have to declare ArrayList Grade10 somewhere else (global?) so it is visible inside the second button event as well? Much appreciate someone showing me the way. Hannes
Hi, it has nothing to do with console app vs windows app. the problem is Grade10 is "not in scope" where you try to use it, otherwise "declaring Grade10 again" would result in a compile-time error. Anyway, declaring again, or performing one more new, never solves anything, since then you are stuck with an object that is different from the original, so does not hold its content, etc. If you want detailed help, publish the actual code, not pieces of it. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
On a windows for, after clicking a button, I add a class Student to an ArrayList with ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(a, b, c, d, e); //Student declared as public class elsewhere This seem to work fine. However, I then want to load the items in a Listbox when another button is clicked. I use foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); I get message that Grade10 does not exist in this context. If I declare Grade10 again in this event it appears that the ArrayList is empty - program runs, no errors, empty ListBox. All samples I find refer to console apps. These work for me but I cannot sort out the windows form - my first attempt at C#, I've been using Delphi for ages. Seems I have to declare ArrayList Grade10 somewhere else (global?) so it is visible inside the second button event as well? Much appreciate someone showing me the way. Hannes
Yep sounds like a scope issue. Not sure where you actually have declared Grade10 and where you've tried to use it but thats a safe bet as to what your problem is, but I can't help you without more code. Sidenote: Use List instead of ArrayList, they're better for you :)
He who makes a beast out of himself gets rid of the pain of being a man
-
On a windows for, after clicking a button, I add a class Student to an ArrayList with ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(a, b, c, d, e); //Student declared as public class elsewhere This seem to work fine. However, I then want to load the items in a Listbox when another button is clicked. I use foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); I get message that Grade10 does not exist in this context. If I declare Grade10 again in this event it appears that the ArrayList is empty - program runs, no errors, empty ListBox. All samples I find refer to console apps. These work for me but I cannot sort out the windows form - my first attempt at C#, I've been using Delphi for ages. Seems I have to declare ArrayList Grade10 somewhere else (global?) so it is visible inside the second button event as well? Much appreciate someone showing me the way. Hannes
Mny thkx so far :) Here's the code I'm using: class Student { string sName; string sSurname; string sID; bool bGender; double dAmount; public Student(string sN, string sS, string sI, bool bG, double dA) { sName = sN; sSurname = sS; sID = sI; bGender = bG; dAmount = dA; } public override string ToString() { return string.Format("Name: {0} Surname: {1} ID: {2} Male: {3} Account: {4}", sName, sSurname, sID, bGender, dAmount); } } private void button3_Click(object sender, EventArgs e) //Click Add button { bool g = false; if (rbBoy.Checked) g = true; //then it is a boy //Now add this student to the ArrayList ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(txtName.Text, txtSurname.Text, txtID.Text, g, Convert.ToDouble(txtAmount.Text))); } private void button2_Click(object sender, EventArgs e) //Click Show button { // ArrayList Grade10 = new ArrayList(); //I'm sure this is cannot be! foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); } Hannes
-
Mny thkx so far :) Here's the code I'm using: class Student { string sName; string sSurname; string sID; bool bGender; double dAmount; public Student(string sN, string sS, string sI, bool bG, double dA) { sName = sN; sSurname = sS; sID = sI; bGender = bG; dAmount = dA; } public override string ToString() { return string.Format("Name: {0} Surname: {1} ID: {2} Male: {3} Account: {4}", sName, sSurname, sID, bGender, dAmount); } } private void button3_Click(object sender, EventArgs e) //Click Add button { bool g = false; if (rbBoy.Checked) g = true; //then it is a boy //Now add this student to the ArrayList ArrayList Grade10 = new ArrayList(); Grade10.Add(new Student(txtName.Text, txtSurname.Text, txtID.Text, g, Convert.ToDouble(txtAmount.Text))); } private void button2_Click(object sender, EventArgs e) //Click Show button { // ArrayList Grade10 = new ArrayList(); //I'm sure this is cannot be! foreach (Student i in Grade10) lbxShow.Items.Add(" " + i); } Hannes
Hi, variables are local to the first {} block that contains their definition, and can not be touched by anything outside this same {} block. how could button2 reach the Grade10 ArrayList that you have defined inside button3_Click??? furthermore, the data structures are all wrong: what would happen if you never click button3 but do click button2. Where would it get its information? you clearly need to buy and study a tutorial on C# and pay attention to local variables, class members, etc. And rethink your program structure. If button3 is intended to add a student to the collection, with information from input boxes, then: - why don't you keep the new student collection alive at the class level? - why don't you add the new student to the lbxShow immediately? (and forget about button2)? :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
-
Hi, variables are local to the first {} block that contains their definition, and can not be touched by anything outside this same {} block. how could button2 reach the Grade10 ArrayList that you have defined inside button3_Click??? furthermore, the data structures are all wrong: what would happen if you never click button3 but do click button2. Where would it get its information? you clearly need to buy and study a tutorial on C# and pay attention to local variables, class members, etc. And rethink your program structure. If button3 is intended to add a student to the collection, with information from input boxes, then: - why don't you keep the new student collection alive at the class level? - why don't you add the new student to the lbxShow immediately? (and forget about button2)? :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.
Thkx for the suggestions, I am indeed wading my way thru a recently purchased 1500-page C# book - while trying to keep up with teaching, examining and marking 200 kids learning Delphi. We have been teaching Delphi at school for some time now (some schools teach Java) and forsee it making way for C#. Questions asked in exams are set so you can answer them in either Delphi or Java. I have set a typical problem - a button to add an object to an ArrayList (Delphi would use a dynamic array, I don't know how Java would handle it), and a button to display the array. Typically, a question would also have buttons to sort and to locate/delete/insert an object. The form design will be given, sometimes even the data. Learners must then make the buttons work. I know global variables defeat the aims of OOP, but I was trying to define the ArrayList as such. So, to some extent, it's not about elegant solutions at this level but rather just getting to grips with the basics. Hannes