473,412 Members | 2,304 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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 12165
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Neno | last post by:
Hi, I have a linkage error that has something to do with the use of a static member array and I can't understand how to solve the problem. Can someone help me please? In detail: A class Month...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
4
by: Mantorok Redgormor | last post by:
Is this legal? int foo = { 0 }; gcc gives: foo.c: In function `main': foo.c:5: warning: missing braces around initializer foo.c:5: warning: (near initialization for `foo') foo.c:5:...
4
by: ccdrbrg | last post by:
I am trying to initialize an arrary of pointers to structs with constants. Sample code: struct mystruct { char *text; int number; };
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
8
by: Sin Jeong-hun | last post by:
Is there any way to initialize an array of objects without looping through each elements? Class A { private int PrivateValue; public A(int a) { PrivateValue=a; }
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.