Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array of objects in C#

Tags:

c#

I have a very beginning C# question. Suppose I have a class called GameObject, and I want to create an array of GameObject entities. I could think of writing code like:

GameObject[] houses = new GameObject[200];

The compiler complains (assuming because of invalid syntax). Since this is XNA development, I load my texture in the LoadContent() method as follows:

 houses[0].Model = Content.Load<Model>("Models\\Building_01 Windowed");

where houses[0] should be a GameObject and can be loaded like this, but the compiler throws this error:

"Use the "new" keyword to create an object instance"

"Check to determine if the object is null before calling the method"

There must be something wrong with my initialization.

like image 878
Kevin Avatar asked Jul 21 '10 16:07

Kevin


People also ask

How do you declare an array of objects in class?

We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.

How do you write an array of objects?

Creating an array of objects We can represent it as an array this way: let cars = [ { "color": "purple", "type": "minivan", "registration": new Date('2017-01-03'), "capacity": 7 }, { "color": "red", "type": "station wagon", "registration": new Date('2018-03-03'), "capacity": 5 }, { ... }, ... ]

How do you create an array of objects in Objective C?

Creating an Array Object For example: NSArray *myColors; myColors = [NSArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; The above code creates a new array object called myColors and initializes it with four constant string objects containing the strings "Red", "Green", "Blue" and "Yellow".

How do you declare an array of 5 elements?

int baz [5] = { }; This creates an array of five int values, each initialized with a value of zero: But, the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}.


3 Answers

The issue here is that you've initialized your array, but not its elements; they are all null. So if you try to reference houses[0], it will be null.

Here's a great little helper method you could write for yourself:

T[] InitializeArray<T>(int length) where T : new()
{
    T[] array = new T[length];
    for (int i = 0; i < length; ++i)
    {
        array[i] = new T();
    }

    return array;
}

Then you could initialize your houses array as:

GameObject[] houses = InitializeArray<GameObject>(200);
like image 109
Dan Tao Avatar answered Oct 06 '22 08:10

Dan Tao


With LINQ, you can transform the array of uninitialized elements into a new collection of created objects with one line of code:

var houses = new GameObject[200].Select(h => new GameObject()).ToArray();

Actually, you can use any other source for this, even generated sequence of integers:

var houses = Enumerable.Repeat(0, 200).Select(h => new GameObject()).ToArray();

However, the first solution seems more readable to me, although the type of the original sequence is not important.

like image 17
Eugene Avatar answered Oct 06 '22 07:10

Eugene


You are creating an array of null references. You should do something like:

for (int i = 0; i < houses.Count; i++)
{
    houses[i] = new GameObject();
}
like image 15
Philip Smith Avatar answered Oct 06 '22 08:10

Philip Smith