Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Display content of an ArrayList in a listbox

Posted on 2010-09-11
5
548 Views
Last Modified: 2013-12-17
Hi Experts,

I am running a very simple program that must get input from a couple of textboxes and store that as a record in an ArrayList and later I want to display every record in a message box and in list box.
The problem I am having is that I am unable to show all record in the ArrayList. Only the last record gets to be display a number of time equivalent to the number of items contained in the ArrayList.
What am I doing wrong?
Below is the code I am using.

Thanks.

public partial class TheForm : Form
    {
        Person myPerson = new Person();
        ArrayList Friends = new ArrayList();

        public TheForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGetName_Click(object sender, EventArgs e)
        {
            myPerson.FirstName = txtFirstName.Text;
            myPerson.LastName = txtLastName.Text;

            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);
            Friends.Add(myPerson);
            
            txtFirstName.Text = "";
            txtLastName.Text = "";

        }

        private void btnShowContent_Click(object sender, EventArgs e)
        {
            //listBoxFriends.DataSource = Friends;
                                    
            foreach (Person myPerson in Friends)
            {
                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());
                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );
                
            }

        }
    }

Open in new window

0
Comment
Question by:Smanyx
  • 3
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33653669
this should work
public partial class TheForm : Form
    {
        ArrayList Friends = new ArrayList();

        public TheForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGetName_Click(object sender, EventArgs e)
        {
var myPerson = new Person();
            myPerson.FirstName = txtFirstName.Text;
            myPerson.LastName = txtLastName.Text;

            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);
            Friends.Add(myPerson);
            
            txtFirstName.Text = "";
            txtLastName.Text = "";

        }

        private void btnShowContent_Click(object sender, EventArgs e)
        {
            //listBoxFriends.DataSource = Friends;
                                    
            foreach (Person myPerson in Friends)
            {
                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());
                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );
                
            }

        }
    }

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 125 total points
ID: 33653681
the problem was the myperson object u declared was global, so when u change it to add a new name the one in the array will also reflect the change...
0
 

Expert Comment

by:RAJESH SHARMA
ID: 33653719
Try This.................

public partial class TheForm : Form
    {
       //  Person myPerson = new Person(); // Remove this line
        ArrayList Friends = new ArrayList();

        public TheForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGetName_Click(object sender, EventArgs e)
        {
        // Create an object of person
           Person myPerson = new Person();

            myPerson.FirstName = txtFirstName.Text;
            myPerson.LastName = txtLastName.Text;

            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);
            Friends.Add(myPerson);
           
            txtFirstName.Text = "";
            txtLastName.Text = "";

        }

        private void btnShowContent_Click(object sender, EventArgs e)
        {
            //listBoxFriends.DataSource = Friends;
                                   
            foreach (Person myPerson in Friends)
            {
                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());
                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );
               
            }

        }
    }
0
 

Author Closing Comment

by:Smanyx
ID: 33653720
Thanks, it works.

Just out of curiosity, why have you used var myPerson and not Person myPerson as I did?
Cheers.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33653793
just lazy i guess :-)... there is no difference between these two lines

Person myPerson = new Person();

and

var myPerson = new Person();

in case of var the type is inferred by what you write on the rhs.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled

789 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