adding to an arraylist

N

NuB

I need some help on this, I'm trying to add to an arrayList and its not
working

here is what i have
public ArrayList carMake
public ArrayList _CarMake { get { carMake; }
set( carMake = value; }}

and i'm adding this to the array List

carMake.Add("BMW", "Lexus", "GMC")

The cars are not being added to the list at all, when i run in debug mode
and step thru it I'm not seeing the values added. what i'm I missing?
 
J

Jon Skeet [C# MVP]

NuB said:
I need some help on this, I'm trying to add to an arrayList and its not
working

here is what i have
public ArrayList carMake
public ArrayList _CarMake { get { carMake; }
set( carMake = value; }}

and i'm adding this to the array List

carMake.Add("BMW", "Lexus", "GMC")

The cars are not being added to the list at all, when i run in debug mode
and step thru it I'm not seeing the values added. what i'm I missing?

Well, for one thing there's no Add method in ArrayList which takes more
than one parameter, so that's clearly not your actual code.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
D

Dave

A few notes inline >>

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
NuB said:
I need some help on this, I'm trying to add to an arrayList and its not working

here is what i have
public ArrayList carMake
This should probably be private access (it's a backing field for a public property)
public ArrayList _CarMake { get { carMake; }
set( carMake = value; }}
Framework standards are to use Pascal case (as you have) without an underscore prefix for public properties
and i'm adding this to the array List

carMake.Add("BMW", "Lexus", "GMC")
Was the arraylist created before calling the Add method? Also, you can only add one item at a time using the Add() method.

Try the following:

private ArrayList carMake = new ArrayList(3); // 3 is initialize capacity, not length

public ArrayList CarMake { get { return carMake; } set { carMake = value; } }

[...]

CarMake.Add("BMW");
CarMake.Add("Lexus");

*or*

CarMake.AddRange(new object[] { "BMW", "Lexus", "GMC" });
 
J

JustCoding

thanks dave that worked.


Dave said:
A few notes inline >>

--
Dave Sexton
[email protected]
-----------------------------------------------------------------------
NuB said:
I need some help on this, I'm trying to add to an arrayList and its not
working

here is what i have
public ArrayList carMake
This should probably be private access (it's a backing field for a public
property)
public ArrayList _CarMake { get { carMake; }
set( carMake = value; }}
Framework standards are to use Pascal case (as you have) without an
underscore prefix for public properties
and i'm adding this to the array List

carMake.Add("BMW", "Lexus", "GMC")
Was the arraylist created before calling the Add method? Also, you can
only add one item at a time using the Add() method.

Try the following:

private ArrayList carMake = new ArrayList(3); // 3 is initialize
capacity, not length

public ArrayList CarMake { get { return carMake; } set { carMake =
value; } }

[...]

CarMake.Add("BMW");
CarMake.Add("Lexus");

*or*

CarMake.AddRange(new object[] { "BMW", "Lexus", "GMC" });
The cars are not being added to the list at all, when i run in debug mode
and step thru it I'm not seeing the values added. what i'm I missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top