initializing array of Array objects

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?
 
Steve,

Nothing really, you just have to loop through and create instances of
arrays to store in your array. If the object is a reference type, then
arrays created of those types will have all elements set to null by default.
It is up to you to create the instance and assign them to the elements in
the array.

Hope this helps.
 
Steve said:
this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?

Absolutely nothing. What did you expect it to do?
 
it has initialised the array (the container) not the contents of the array.

if you want the contents to be initialised with a instance on an object then
you are going to have loop over the array and create the objects manually.

HTH

Ollie Riches
 
allocate 3 Array objects ;)

C++
MyClass* pClasses = new MyClass[3];

I know this isn't C++, but I'm just used to things working that way. My
bad, lesson learned ;)


Jon Skeet said:
Steve said:
this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?

Absolutely nothing. What did you expect it to do?
 
Thanks Nicholas, I understand now.


Nicholas Paldino said:
Steve,

Nothing really, you just have to loop through and create instances of
arrays to store in your array. If the object is a reference type, then
arrays created of those types will have all elements set to null by default.
It is up to you to create the instance and assign them to the elements in
the array.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Steve said:
this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?
 
You have created an Array of the type Array called m_arrays...
The array is not initialized and the type (Array) is a reference type..so
the value is null...

//The following code should explain it...
Array[] m_arrays = new Array[3];
Array[] firstArray = new Array[10];
Array[] secondArray = new Array[10];
Array[] thirdArray = new Array[10];
m_arrays[0] = firstArray;
m_arrays[1] = secondArray;
m_arrays[2] = thirdArray;


//An Array of Integers..initialized to 1, 2, 3
int[] myArray = new int[] { 1, 2, 3 };

//An other way
int[] myOtherIntArray = new int[3];
for (int i = 0; i < myOtherIntArray.Length; i++)
myOtherIntArray = i + 1;

/Oscar
 
Hi,

It's been a while since I last worked in C but , IIRC the code below does
not do what you want, it does create a new array of MyClass with 3
elements.
It does not create a array of arrays.
Again, IIRC that would be like:
MyClass** pClasses = new MyClass[3][];


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Steve said:
allocate 3 Array objects ;)

C++
MyClass* pClasses = new MyClass[3];

I know this isn't C++, but I'm just used to things working that way. My
bad, lesson learned ;)


Jon Skeet said:
Steve said:
this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?

Absolutely nothing. What did you expect it to do?
 
Steve said:
this code:
private Array[] m_arrays = new Array[3];

results in an array of 3 null Array objects. What am I missing?

First off, it looks like you're creating an array of array objects. In
C# Array is a type. If you don't want your objects to be null you need
to give an array intializer. Ex:

int[] myAr = new int[3] {0, 0, 0}; //set all values to 0

if you want to create an array of array objects like you seem to be
doing in your code, then...

Array[] myAr = new Array[] {new Array[3], new Array[3], new Array[3]};

but i don't think that's what you ment.
 
Back
Top