You can store anything in an ArrayList and this is common practice where a
single-dimensioned collection that only needs to be addressed via numeric
index is appropriate. If the number of items is known and development time,
an array is more performant, but if you need dynamic sizing, an ArrayList is
the way to go.
ArrayList aList = new ArrayList();
aList.Add(new MyClass()); // "element" 0
aList.Add(new MyClass()); // "element" 1
MyClass myClassInstance = (MyClass)aList[0]; // retreive element 0
MyClass myOtherInstance = (MyClass)aList[1]; // retreive element 1
If you want to avoid the casting in your client code, derive a typed
collection from CollectionBase -- it will work about the same except that
the items in the collection will be of your specific type rather than simply
System.Object.
As I mentioned before, with CLR 2.0 / VS 2005 you can use List<T> to make it
more performant and type-safe:
List<MyClass> aList = new List<MyClass>();
aList.Add(new MyClass());
aList.Add(new MyClass());
MyClass myClassInstance = aList[0];
MyClass myOtherInstance = aList[1];
.... and so forth. This client code storage and retreival logic looks the
same as a CLR 1.1 implementation derived from CollectionBase, but the latter
just *hides* the casting, whereas a generic List<T> actually *is* of your
custom type.
If you want more of an array syntax you could derive a wrapper class from
ArrayList or List<T> and then you could pretend it was a resizeable array:
SizeableArray<MyClass> aList = new SizeableArray<MyClass>(2);
aList[0] = new MyClass();
aList[1] = new MyClass();
aList.Resize(3);
aList[2] = new MyClass();
// etc.
--Bob
"John Grandy" <johnagrandy-at-yahoo-dot-com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi Cor, and thanks for the response.
How do you define "simple array". For example, would it be wise to store
instances of my custom class in an ArrayList ?
"Cor Ligthert" <no************@planet.nl> wrote in message
news:O%****************@TK2MSFTNGP12.phx.gbl... John,
You would not want to use it in both of the languages.
Use as already told by others when it is a simple array an arraylist and
in all other cases an array/collection that implements ilist or
icollection. You can create one as well easy yourself using
collectionbase.
http://msdn.microsoft.com/library/de...classtopic.asp
I hope this helps,
Cor