C/C++ Array of Objects

usetheforce2
01-09-2002, 06:52 PM
hey all,

i know, i know, this is a vb site but, well, you know!

i'm new to c# and i was wondering if there's a way to simulate redimensioning object arrays in the same way vb allows: I realize that i can set a predefined amout of elements for the object array

Object myObject[10];


but i like to be able to increase the array:
ie:

[Visual Basic]

dim myObject() as Some_Kind_of_Object

redim preserver myObject(elements + 1)


or will i have to use a collection of objects in C# rather than redimensioning?

thanks, hopefully thats not to confusing.

Regan

Squirm
01-09-2002, 07:01 PM
Well, in C++ you use the 'New' and 'Delete' keywords to create and destroy dynamically:

char* String;
String = char* new char[Length];
Delete [] String;

reboot
01-10-2002, 08:27 AM
c# has New like c++

New doesn't preserve however. I'm not sure how you do that.
Array.Copy maybe? I'm barely familiar with c# myself.

reboot
01-10-2002, 08:50 AM
Here's a redim for c#

Credits to Justin Spindler (programmer buddy of mine)

public static System.Array redim(System.Array source, int size){if(source!=null){System.Array dest = System.Array.CreateInstance(source.GetValue(0).GetType(), size); source.CopyTo(dest,0); return dest;} else{ throw new System.ArgumentNullException("source", "Source must contain initialized array."); }


int[] x = new int[5] = {1,2,3,4,5}; x = (int[])redim(x, 10);

usetheforce2
01-10-2002, 01:52 PM
thank you people,

I'll give your ideas and examples a go and see what i come up with!

Regan

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum