views:

368

answers:

3

Im working on a project for homework where I have an Arraylist containing objects of 5 strings. I know how to select items of the array list (using an index value) but not how to access the objects strings. Any help would be great (I'm not trying to cheat but its getting frustrating that I cant't sort it out. I hope this snippets make it obvious what i'm trying to do.

private ArrayList myComponents;

private int listIndex = 0;

myComponents = new ArrayList(); //Arraylist to hold catalog data

equipment = new Equipment(itemName, itemType, itemDetails, itemMaintenance, itemId);

myComponents.Add(equipment);

// class file is called Equipment.cs

//I know normally that equipment without the arraylist this would work: equipment.getitemName(); but combining with the arraylist is being problematic.

A: 

Since all the items in the array list are "objects" on the face of it, but they're actually Equipment objects under the covers, you need a way of going from object to Equipment when retrieving the items from the ArrayList (hint: Cast). Don't want to give it away since this is homework, but that should help....

BFree
since this is c#, don't use the (object) cast type. There is another keyword that you should use.
Woot4Moo
+1  A: 

You will likely be better off using a List instead of an ArrayList. An ArrayList is not strongly typed which means that you cannot treat the things/objects inside the array like they are "Equipment" but rather only like they are a generic boring object.

List<Equipment> myComponents = new List<Equipment> ();

equipment = new Equipment(itemName, itemType, itemDetails, itemMaintenance, itemId);

myComponents.Add(equipment);

foreach(Equipment eq in myComponents)
{
    eq.getItemName();
    // do stuff here
}

Let me know if this solves your problem.

Polatrite
+1  A: 

An ArrayList doesn't know (or care) what kind of objects are placed in it. It treats everything put into it as an Object. When retrieving objects from the ArrayList you will need to convert the returned Object reference into a reference of the appropriate type before you can access that types properties and methods. There are several ways of doing this:

// this will throw an exception if myComponents[0] is not an instance of Equipement
Equipment eq = (Equipment) myComponents[0]; 

// this is a test you can to to check the type
if(myComponents[i] is Equipment){
  // unlike the cast above, this will not throw and exception, it will set eq to
  // null if myComponents[0] is not an instance of Equipement
  Equipment eq = myComponents[0] as Equipment;
}

// foreach will do the cast for you like the first example, but since it is a cast
// it will throw an exception if the type is wrong.
foreach(Equipment eq in myComponents){
    ...
}

That being said, if possible, you really want to be using a generic type. The one that works most like an ArrayList is List. Generics help in a lot of cases to avoid all the casting that makes the ArrayList code painful to write and error prone. The downside is of course that you cannot mix types in a List. A List won't let you put a string in it, while an ArrayList full of Equipment instances will. The particular problem you are trying to solve will determine which makes more sense.

Dolphin