468,727 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,727 developers. It's quick & easy.

initializing array of Array objects

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

results in an array of 3 null Array objects. What am I missing?
Nov 17 '05 #1
10 11655
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]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
this code:
private Array[] m_arrays = new Array[3];

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

Nov 17 '05 #2
Steve <ss*@sss.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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
"Steve" <ss*@sss.com> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
this code:
private Array[] m_arrays = new Array[3];

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

Nov 17 '05 #4
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve <ss*@sss.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #5
Thanks Nicholas, I understand now.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O$****************@TK2MSFTNGP10.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Steve" <ss*@sss.com> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
this code:
private Array[] m_arrays = new Array[3];

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


Nov 17 '05 #6

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] = i + 1;

/Oscar

"Steve" <ss*@sss.com> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
this code:
private Array[] m_arrays = new Array[3];

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

Nov 17 '05 #7
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" <ss*@sss.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
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 [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steve <ss*@sss.com> wrote:
> 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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Nov 17 '05 #8
Steve wrote:
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.
--
Rob Schieber
Nov 17 '05 #9
This may help:

Chapter 5 "C++ and Java Gumption Traps"
http://www.geocities.com/Jeff_Louie/OOP/oop5.htm

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #10
Thanks all, everything is very clear now, up and running. :0)
"Steve" <ss*@sss.com> wrote in message
news:e9**************@TK2MSFTNGP10.phx.gbl...
this code:
private Array[] m_arrays = new Array[3];

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

Nov 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by simondex | last post: by
4 posts views Thread by Mantorok Redgormor | last post: by
4 posts views Thread by ccdrbrg | last post: by
6 posts views Thread by alacrite | last post: by
13 posts views Thread by WaterWalk | last post: by
4 posts views Thread by Peskov Dmitry | last post: by
8 posts views Thread by Sin Jeong-hun | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
xarzu
1 post views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.