Collections

DevScript

Newcomer
Joined
Dec 30, 2009
Messages
1
I have a class called Product and a ArrayList called Products:

Code:
public class Product
{
string strName;

   
   public Product(string newName)
    {
            strName = newName;
     }

   public void DoStuff()
   {

   }
}

So I put the Product in the ArrayList:

Code:
ArrayList Products = New ArrayList();
Products.Add(new Product("Coke"))

How I can do this? (below)

Code:
Products[0].DoStuff();

Thanks.
 
You would either need to cast the element to the correct type i.e.
Code:
((Product) Products[0]).DoStuff();
or if you are using .Net 2 and above you may be better of using a generic class such as the List class. i.e.
Code:
List<Product> Products = new List<Product>();
Products.Add(new Product("Coke"));

Products[0].DoStuff(); //should work
 
Back
Top