C# Initializing an Array of Class Instances?
I'm very new at C# and what I'm trying to do is beyond my skill level. But the best way to learn is to try right? I've created my own class, it doesn't do much, but that's ok, I can add more functionality later. The class is called 'Entity'. I want to create an array of instances of 'Entity' that I can then access later on in the program. Let's say I want to create an array of 100 instances.
How do I create AND initialize an array of 100 instances of my Entity class?
So far I have tried every way I can find in books and online and none of them work. I know this... (I think)
int[] pins; // DECLARES an array of type integer
pins = new int[4] // CREATES and INITIALIZES an array of type integer
But when I try to apply the same rules to my entity class I get error messages I can't understand.
int maxEntities = 100;
Entity[] entities = new Entity[maxEntities];
...gives me an error 'A field initializer cannot reference the nonstatic field, method, or property 'EntityTest1.Form1.maxEntities''
So clearly I am approaching this from the wrong angle. Any help (VERY SIMPLE HELP PLEASE) would be appreciated, thanks!
EDIT: I think I've discovered the problem. The problem is one of scope. Now I'm trying to figure out where in the windows form program to create my array. I think the problem is I've been doing it in the wrong place
[Edited by - Theodore Fuhringer on October 8, 2008 1:23:26 PM]
You are trying to initialize the array called 'entities' where it is declared; you cannot do this if you are going to use another field as the size the array, because that field may not be initialized yet (you could do it just fine with a compile time constant, however). If you must use a field, create the entities array in you class's constructor instead:
class T { int maxEntities = 100; Entity[] entities; // Constructor: public T() { entities = new Entity[maxEntities]; }}
Wait, I can create the array INSIDE the class? What a great idea! That solves so many problems. I just didn't think you could create an instance of a class inside a class itself. So much I don't know. Thanks!
You're right about the uninitialized field being the problem. When I change it to...
Entity[] entities = new Entity[100];
It works just fine. Heh.
You're right about the uninitialized field being the problem. When I change it to...
Entity[] entities = new Entity[100];
It works just fine. Heh.
Quote:Original post by Theodore Fuhringer
Entity[] entities = new Entity[100];
Just remember that this line doesn't create any Entity objects, only 100 references of type Entity.
Quote:Original post by DevFredQuote:Original post by Theodore Fuhringer
Entity[] entities = new Entity[100];
Just remember that this line doesn't create any Entity objects, only 100 references of type Entity.
And I just hit that problem LOL. I got the following to work...
int maxEntities = 100;
Entity[] entities;
entities = new Entity[maxEntities];
...but when I then tried to do
entities[1].initialize(1, "Jim", 1);
...the program barfs. Says 'Object reference not set to an instance of an object.'
So the line 'entities = new Entity[maxEntities];' doesn't run the constructor and create instances of Entity?
IT'S WORKING!
My code is now...
... and it works fine. Thanks for the help jpetrie and DevFred. Talking it through helped enormously. I love this forum. :)
My code is now...
int maxEntities = 100; Entity[] entities; entities = new Entity[maxEntities]; for (int i = 0; i != maxEntities; i++) { entities = new Entity(); } entities[1].initialize(1, "Jim", 1);
... and it works fine. Thanks for the help jpetrie and DevFred. Talking it through helped enormously. I love this forum. :)
Depending on what your doing a collection could be a better solution since you can create them and initialize them at the same time and add them to the collection as you please without having to worry out null exceptions.
List<Entity> entitys = new List<Entity>();
entitys.add(new Entity(1, "Jim", 1));
entitys.add(new Entity(3, "Bob", 2));
List<Entity> entitys = new List<Entity>();
entitys.add(new Entity(1, "Jim", 1));
entitys.add(new Entity(3, "Bob", 2));
Quote:Original post by Kaze
Depending on what your doing a collection could be a better solution since you can create them and initialize them at the same time and add them to the collection as you please without having to worry out null exceptions.
List<Entity> entitys = new List<Entity>();
entitys.add(new Entity(1, "Jim", 1));
entitys.add(new Entity(3, "Bob", 2));
You may be right Kaze, but I only just wrote my first solo C# program a few days ago so I'm not quite there yet.
Fortunately I don't think null exceptions will be a problem since I want all the entities, including ones not yet populating the world, to exist before the game begins.
New problem. I want to run the creation of the entities array at the beginning of the program so that whenever a user clicks one of the buttons the array is accessible. So far I put the creation inside a button click event and the array only exists inside that event. How to I make the array accessible to the entire form1 program?
I tried putting in in the form1_load event but I still get the error 'The name 'entities' does not exist in the current context'
EDIT: SOLVED: I put the declaration at the top of the 'public partial class Form1 : Form' section and put the initialization in the button1_click method and it works fine now. Thanks for the help!
It occurred to me that declaration and initialization are two different things and maybe could be done in different places. Guess I was right!
[Edited by - Theodore Fuhringer on October 7, 2008 6:00:35 PM]
I tried putting in in the form1_load event but I still get the error 'The name 'entities' does not exist in the current context'
EDIT: SOLVED: I put the declaration at the top of the 'public partial class Form1 : Form' section and put the initialization in the button1_click method and it works fine now. Thanks for the help!
It occurred to me that declaration and initialization are two different things and maybe could be done in different places. Guess I was right!
[Edited by - Theodore Fuhringer on October 7, 2008 6:00:35 PM]
move
Entity[] entities;
outside the functions but inside the from class
EDIT:
int maxEntities = 100;
Entity[] entities;
public void FormLoad(){
entities = new Entity[maxEntities];
for (int i = 0; i != maxEntities; i++)
{
entities = new Entity();
}
entities[1].initialize(1, "Jim", 1);
}
Entity[] entities;
outside the functions but inside the from class
EDIT:
int maxEntities = 100;
Entity[] entities;
public void FormLoad(){
entities = new Entity[maxEntities];
for (int i = 0; i != maxEntities; i++)
{
entities = new Entity();
}
entities[1].initialize(1, "Jim", 1);
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement