views:

271

answers:

7

I'm aware that an ArrayList is probably not the way to go with this particular situation, but humor me and help me lose this headache.

I have a constructor class like follows:

class Peoples
    {
        public string LastName;
        public string FirstName;
        public Peoples(string lastName, string firstName)
        {
            LastName = lastName;
            FirstName = firstName;
        }
    }

And I'm trying to build an ArrayList to build a collection by calling on this constructor. However, I can't seem to find a way to build the ArrayList properly when I use this constructor. I have figured it out with an Array, but not an ArrayList.

I have been messing with this to try to build my ArrayList:

ArrayList people = new ArrayList();
            people[0] = new Peoples("Bar", "Foo");
            people[1] = new Peoples("Quirk", "Baz");
            people[2] = new Peopls("Get", "Gad");

My indexing is apparently out of range according to the exception I get.

+8  A: 

It should be:

people.Add(new Peoples(etc.));

instead of

people[0] = new people()...;

Or better yet:

List<People> people = new List<People>();

people.Add(new People);

Just to be complete. Using a straight array:

People[] people = new People[3];

people[0] = new People();
Kevin
great, thanks! sorry that I haven't quite figured out how to format my posts yet. if anybody could send me a message with some tips, that'd be great.
EvanRyan
@Evan I would start here: http://stackoverflow.com/editing-help
Kevin
Thanks for the help guys. The only reason for doing it this way is that it's for a school project, and I'm required to go through all the different types (Array, ArrayList, List, etc.). But I greatly appreciate the tips on the better ways to go about it.
EvanRyan
+1 Kevin you got there first - just!
ChrisBD
+2  A: 

You should add elements to the list. Like the following

ArrayList people = new ArrayList(); 
people.Add(new Peoples("Bar", "Foo"));
Anthony Pegram
+1  A: 

You should use the ArrayList.Add function to add to the array list.

ArrayList peoplesArray = new ArrayList();
peoplesArray.Add(new Peoples("John","Smith");
Miky Dinescu
+2  A: 

You need to do

people.Add (new Peoples("Bar", "Foo"));
people.Add (new Peoples("Quirk", "Baz"));
people.Add (new Peoples("Get", "Gad"));
Guido Domenici
+3  A: 

Try people.Add(new Peoples("Bar", "Foo");

juharr
+2  A: 

When you attempt to call people[i] without first populating the array list you will get the IndexOutOfRangeException. You must first add to the ArrayList.

ArrayList list = new ArrayList();
list.Add(new Peoples("Bar", "Foo"));

You can then access the list by the index which would be done in a foreach or for loop.

Is there a reason you are not using List<Peoples> which would give you a strongly typed collection? Also, you have publicly accessible fields in the class although I realise you probably just threw together that code for the question.

Peter Kelly
A: 

FYI, ArrayList is considered evil by many. As Kevin said, it would be better to us List<People>.

List is what is called a generic. Google 'strongly typed', 'boxing', and 'generics' for a better understanding of why.

back to your original question: An array's size must be declared when instantiated, i.e. People[] people = new People[5];

this creates 5 empty cells in the array so you can access the cells using a subscript i.e. [0]

An ArrayList or List<T> when instantiated using the default constructor has no cells i.e. List<People> people = new List<People>();

people[0] does not exist at this point.

use people.Add(new People("first", "last")); to add a new cell to the list. now the subscript [0] is valid, but [1] is still invalid because there is only one cell.

A list i.e. ArrayList or List can grow dynamically by using .Add(). Once added to a list, you can reference them using the subscript [i], but you cannot use the subcript to add them.

Necroposter