Initialising objects in an array

  • Thread starter Thread starter Mike Windsor
  • Start date Start date
M

Mike Windsor

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
 
Mike Windsor said:
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
 
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 = new Foo(i);

for (int i = 0; i < aFoo.Length; i++)
Console.WriteLine(string.Format("index = {0}", aFoo.index));
 
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.
 
Chris R said:
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
 
Back
Top