473,411 Members | 2,185 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,411 software developers and data experts.

Initialising objects in an array

This seems like a fairly basic thing to want to to, but as far as I can
tell, it's not possible. Can anyone help?

I have a class 'Foo' and I want to call the constructor with a particular
(identical) value for each object in an array of 'Foo's:

public class Foo
{
public Foo(bool val)
{
// Some code
}
}

....

Foo[] arr1 = new Foo[100](true); // This doesn't work, but it
represents what I want to do
Foo[] arr2 = new Foo(false)[250]; // Likewise...

Is there any way around this, short of defining a new method,
'Initialise(bool val)' and moving the initialisation code to there from the
original constructor?

Thanks for your help.

Cheers,
Mike
Nov 15 '05 #1
4 1523
"Mike Windsor" <mj*************@THIShotmail.com> wrote in message
news:rG***************@newsfep3-gui.server.ntli.net...
This seems like a fairly basic thing to want to to, but as far as I can
tell, it's not possible. Can anyone help?

I have a class 'Foo' and I want to call the constructor with a particular
(identical) value for each object in an array of 'Foo's:

public class Foo
{
public Foo(bool val)
{
// Some code
}
}

...

Foo[] arr1 = new Foo[100](true); // This doesn't work, but it
represents what I want to do
Foo[] arr2 = new Foo(false)[250]; // Likewise...

Is there any way around this, short of defining a new method,
'Initialise(bool val)' and moving the initialisation code to there from the original constructor?

Thanks for your help.

Cheers,
Mike


I had the same problem just now. I wanted to make an array of classes and
ended up having to make the array and then assign a class to each term in
the array. I used a for loop to do that. so I guess you could try that. but
if someone else can suggest a better way I want to know
dave
Nov 15 '05 #2
On Sun, 23 Nov 2003 20:22:22 -0000, in microsoft.public.dotnet.languages.csharp
you wrote:
public class Foo
{
public Foo(bool val)
{
// Some code
}
}


You basically have to create an array of the class type and then assign each
array element in for() loop.

I just tried it out and did the following with no problems.
class Foo
{
public int index = 0;

public Foo(int index)
{
this.index = index;
}
}

Foo[] aFoo = new Foo[10];
for (int i = 0; i < aFoo.Length; i++)
aFoo[i] = new Foo(i);

for (int i = 0; i < aFoo.Length; i++)
Console.WriteLine(string.Format("index = {0}", aFoo[i].index));

Nov 15 '05 #3
What you need to realize here is that
Foo[] arr1 = new Foo[100];
does not create any Foo objects. Rather, it creates a type safe array that
still needs to be filled with Foo objects. That is why you need a for loop
or similar initialization routine to create the Foo objects. At this point,
you will have 100 null reference values, so you cannot run the constructor
on the nulls.

Chris R.

"Mike Windsor" <mj*************@THIShotmail.com> wrote in message
news:rG***************@newsfep3-gui.server.ntli.net...
This seems like a fairly basic thing to want to to, but as far as I can
tell, it's not possible. Can anyone help?

I have a class 'Foo' and I want to call the constructor with a particular
(identical) value for each object in an array of 'Foo's:

public class Foo
{
public Foo(bool val)
{
// Some code
}
}

...

Foo[] arr1 = new Foo[100](true); // This doesn't work, but it
represents what I want to do
Foo[] arr2 = new Foo(false)[250]; // Likewise...

Is there any way around this, short of defining a new method,
'Initialise(bool val)' and moving the initialisation code to there from the original constructor?

Thanks for your help.

Cheers,
Mike

Nov 15 '05 #4
"Chris R" <so*****@hotmail.com> wrote in message
news:Oh**************@TK2MSFTNGP11.phx.gbl...
What you need to realize here is that
Foo[] arr1 = new Foo[100];
does not create any Foo objects. Rather, it creates a type safe array that still needs to be filled with Foo objects. That is why you need a for loop or similar initialization routine to create the Foo objects. At this point, you will have 100 null reference values, so you cannot run the constructor
on the nulls.


Yep, I discovered this as soon as I tried to run the program and got a null
reference exception... not what I was expecting!

I guess this is the price of losing pointers (and I'm assuming derivation
from C++ here) - there would be no other way of instantiating an array of
place holders if you didn't want the objects to all be created at the same
time.

Thanks for everybody's help. I used a for loop in the end, which is what a
couple of people suggested.

Cheers,
Mike
Nov 15 '05 #5

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

Similar topics

1
by: ajit goel | last post by:
Hi; I have a question which respect to initialising and creating objects in loops.Here are the 2 scenarios: 1. ###############################################################################...
8
by: Nick Keighley | last post by:
Hi, is there a way of initialising a vector in a similar fashion to initialising an array? consider: Shape* triangle = new Triangle (); Shape* square = new Square (); Shape* shape_arr =...
6
by: Jeremy Targett | last post by:
Hello, I'm trying to initialise a two-dimensional array of characters, 12 by 60, with each entry maximum 6 characters wide. (It's a fix I'm making to an old program which I learned just enough C...
3
by: mark.bergman | last post by:
Running lint on code initialising a local 2-D array gives a warning. void f(void) { int a = { 0 }; ... lint gives "warning: Partially elided initialisation..." Should this be happening,...
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
2
by: Christoph Conrad | last post by:
Hi, given the following test case, with CString from Microsofts MFC: ================================================== file 1: CString arr; ...
9
by: Jim | last post by:
Hi, I want to declare that that a valarray of a certain name exist at the beginning of some code, but I can't instatiate it until I've read in some parameters later on in a for loop i.e. int...
4
by: Nick Keighley | last post by:
Hi, is this correct:- typedef struct { long frequency; } Structure; Structure my_struct = {0};
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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.