1

I have a class Measurement.

I have a constructor inside this class. as:

class Measurement
    {
      public Measurement(MainWindow mainWindow)
        {
            ....
        }
}

How can I create an Array of 8 Objects with the MainWindow Parameter?

Like somewhere in my code:

Measurement[] measurements= new Measurement[8](mainWin); 
2
  • 2
    And what do you expect "new Measurement[8](mainWin); " would do? Commented Jun 18, 2013 at 14:20
  • I have 8 drawings with different properties Commented Jun 18, 2013 at 14:28

4 Answers 4

15

Do you want an array with a single reference 8 times, or 8 separate Measurement objects?

For the first:

var measurements = Enumerable.Repeat(new Measurement(mainWin), 8).ToArray();

For the second:

var measurements = Enumerable.Range(0, 8)
                             .Select(_ => new Measurement(mainWin))
                             .ToArray();

(Or just create an array without initializing the elements, then populate it with a loop, of course. It's a matter of personal preference.)

Sign up to request clarification or add additional context in comments.

8 Comments

Well done for pointing out the behaviors of both methods, but in what situation might one want an array of 8 (or any number) references to the same object? Or do you mean that the MainWindow is the object that is referenced multiple times? and there is infact 8 seperate instances of Measurement?
@BhushanFirake It's the name of the parameter for the anonymous method; in this case the number from the call to Range. Using _ as the name of a variable that is unused is a common convention.
I've never seen a Select overload that accepts a 2nd parameter. What does the , 8 do?
@musefan: Imagine an immutable type for the state of a space on a game board. Given immutability, it's fine to populate the whole board with the same reference to the initial state - then any changes to the state of the board would change the array element to refer to the new state. It's relatively rare, but I wanted to draw attention to the two different potential meanings.
You could also Enumerable.Repeat(mainWin, 8).Select(mw => new Measurement(mw)).ToArray();.
|
14

You can use LINQ:

var measurements = Enumerable.Range(0, 8).Select(i => new Measurement(mainWin)).ToArray();

A second way is to use the array initializer syntax:

var measurements = new[] {
    new Measurements(mainWin), new Measurements(mainWin), 
    new Measurements(mainWin), new Measurements(mainWin), 
    new Measurements(mainWin), new Measurements(mainWin), 
    new Measurements(mainWin), new Measurements(mainWin)
};

1 Comment

@user2261524 If it is the perfect answer as you say, you should mark it as "correct".
3

There is this way (by using Enumerable.Repeat) :

var measurements = Enumerable.Repeat(new Measurement(mainWin), 8).ToArray(); 

Quote :

Generates a sequence that contains one repeated value.

3 Comments

This will only create 1 Measurement object, not 8. That's probably not desirable.
Yes it will. But since all have already answered the correct way in filling the array i felt like adding this aswell. Have added a quote from the link included in the answer. Thanks.
Since several correct methods have been added you decided to add an incorrect method? That doesn't really make sense. Note that it may not be obvious to all readers that this is repeating a reference to the object, and not repeating a call to the constructor.
2
Measurement[] measurements= new Measurement[8];

for(int i = 0; i < measurements.Length; i++)
{
   measurements[i] = new Measurement(mainWin);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.