473,508 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need an array of object pointers

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
Nov 16 '05 #1
3 14677
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.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
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" <Mo************@hotmail.com> wrote in message
news:opsdrrnp1mklbvpo@stone...
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.

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3
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[i].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[i]).DoSomething(). ]

--Bob

"Jack Addington" <sh******@hotmail.com> wrote in message
news:ea*************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1927
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
67
4389
by: Ike Naar | last post by:
Hi, Asking your advice on the following subject: Suppose I want to find out whether a given pointer (say, p) of type *T points to an element of a given array (say, a) of type T. A way to...
9
2599
by: buda | last post by:
Hi, I've been wondering for a while now (and always forgot to ask :) what is the exact quote from the Standard that forbids the use of (&array) (when x >= number_of_columns) as stated in the FAQ...
23
2501
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
24
3403
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
18
2307
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before....
20
1688
by: subramanian | last post by:
Hello I have a doubt in the following piece of code: int a; printf("a=%p\n", a); printf("&a=%p\n", &a); these printf statements print the same value for both 'a' and '&a". I tried in...
2
2963
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
11
4617
by: memeticvirus | last post by:
I have an array cli::array<float, 2and I would like to access a subset of it's values by compiling an array of pointers. But, it's not possible to create an array of type...
152
9734
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.