Need an array of object pointers

  • Thread starter Thread starter Jack Addington
  • Start date Start date
J

Jack Addington

Quite new to C# but I am getting quite confused with Array's and ArrayLists.
This is probably the same old iteration of a basic question but I can't seem
to find a clear answer/example of this case.

Basically I want a container that points to a set objects that I can then
interate through to group together a set of methods in a transaction. The
number of objects is unknown at compile time. I can't seem to come up with
a dynamic array of object references... only copies. I don't want 2
versions of the object. I'm getting confused between ArrayList and the need
to dynamically re-allocate the array each time I need to add a new object.

-------------
ie) object A = new object();
object B = new object();
object C = new object();

object[] list;

then how do I increase the size of the array so I can do:

object[1] = &A;
object[2] = &B;
object[3] = &C;

then my goal is to do

foreach( object l in list )
l.Save( )
---------------

Am I missing something with ArrayList? It isn't clear to me that
ArrayList.Add( object ) doesn't create another copy...

thx
 
Hi Jack,

In C# things are handled by references, which is similar to pointers.

ArrayList.Add(myObject) adds myObject to the list, but myObject is just a reference not the object itself.

When creating an object array (object[]) you have to specify the size of it before you can use it.

object A = new object();
object B = new object();
object C = new object();

A, B and C are references to three object objects (the object class can get confusing to explain)

object[] list = new object[3];
list[0] = A; // arrays always start at position 0 in C#
list[1] = B;
list[2] = C;

list will now contain three references

If you don't know the size of the array it would be better to use an ArrayList

ArrayList list = new ArrayList();
list.Add(A);
list.Add(B);
list.Add(C);

list will now also contain three references

foreach(object o in list)
{
o.Save // except object doesn't have a method called save.
}

Once an object is created you handle it by passing its reference around.
 
perfect thanks... I managed to convince myself that ArrayList wasn't what I
wanted when all along it was perfect. As for the .Save() I was just making
an example... MyObject would have been a better thing to type up that
Object.

thanks again

jack
Morten Wennevik said:
Hi Jack,

In C# things are handled by references, which is similar to pointers.

ArrayList.Add(myObject) adds myObject to the list, but myObject is just a
reference not the object itself.
When creating an object array (object[]) you have to specify the size of it before you can use it.

object A = new object();
object B = new object();
object C = new object();

A, B and C are references to three object objects (the object class can get confusing to explain)

object[] list = new object[3];
list[0] = A; // arrays always start at position 0 in C#
list[1] = B;
list[2] = C;

list will now contain three references

If you don't know the size of the array it would be better to use an ArrayList

ArrayList list = new ArrayList();
list.Add(A);
list.Add(B);
list.Add(C);

list will now also contain three references

foreach(object o in list)
{
o.Save // except object doesn't have a method called save.
}

Once an object is created you handle it by passing its reference around.
 
What is your target release date? Because Visual Studio 2005 (CLR 2.0) will
have generics, which gives you the List<T> class, which provides the dynamic
sizing benefits of the ArrayList without all the casting overhead;

using System.Collections.Generics;

List<MyClass> list = new List<MyClass>();

list.Add(new MyClass());
list.Add(new MyClass());

for (i = 0;i < list.Count;i++) {
list.DoSomething();
}

[ Unless things change in CLR 2.0, for is faster than foreach, but not
necessarily enough to worry about in practice most of the time; but I use it
here to more clearly illustrate that this is truly a list of MyClasses, and
doesn't have to be cast back from object. If you used a for loop with
ArrayList you'd have to do ((MyClass)list).DoSomething(). ]

--Bob
 
Back
Top