ArrayList, can't add ?

  • Thread starter Thread starter vertigo
  • Start date Start date
V

vertigo

Hello
When i try:

MyClass myClassObject = new MyClass();
ArrayList myArrayList;
myArrayList.Add(myClassObject);

where MyClass is my own class What's wrong ?


Thanx
Michal
 
ArrayList myArrayList = new ArrayList();

or

You could go a step further and create a myClassObjectCollection class:

public class MyClassCollection : System.Collections.CollectionBase
{
// implement the methods you need here, such as: Add, Remove, etc...
}

MyClass myClassObject = new MyClass();
MyClassCollection myClassObjects = new MyClassCollection();

myClassObjects.Add(myClassObject);
 
Michal,

The reason this doesn't work is because you haven't created an array
list to place your class in. The line that reads:

ArrayList myArrayList;

Should read:

ArrayList myArrayList = new ArrayList();

Hope this helps.
 
vertigo said:
Hello
When i try:

MyClass myClassObject = new MyClass();
ArrayList myArrayList;
myArrayList.Add(myClassObject);

where MyClass is my own class What's wrong ?


Thanx
Michal


you have to create new arraylist

Try this code
MyClass myClassObject = new MyClass();
ArrayList myArrayList = new ArrayList()
myArrayList.Add(myClassObject);


Regards
sambath R
 
Back
Top