C# Initializing an Array of Class Instances?

Started by
15 comments, last by Zahlman 15 years, 6 months ago
Thanks Kaze! Problem solved.

Don't worry though. I'll probably have more questions tomorrow lol.
Advertisement
PS. Please don't mark your thread as 'solved'!
Quote:Original post by Theodore Fuhringer
entities[1].initialize(1, "Jim", 1);

An initialize method? That's what constructors are for. In your current solution, it's possible to create Entities that aren't initialized - not very OO.
Quote:Original post by DevFred
Quote:Original post by Theodore Fuhringer
entities[1].initialize(1, "Jim", 1);

An initialize method? That's what constructors are for. In your current solution, it's possible to create Entities that aren't initialized - not very OO.


The initialize method is meant to be called by the main program to generate a specific kind of entity, when one is needed, since there are dozens of types within the class. The default constructor only generates a 'starting' entity. Initialize generates a 'mature' entity.

That's why I did it that way. But I'll try to think of a way to do it via the constructor. It'll be an interesting challenge.

Thanks!

P.S. Why are we not supposed to mark questions as SOLVED?
Quote:
The initialize method is meant to be called by the main program to generate a specific kind of entity, when one is needed, since there are dozens of types within the class. The default constructor only generates a 'starting' entity. Initialize generates a 'mature' entity.

That's why I did it that way. But I'll try to think of a way to do it via the constructor. It'll be an interesting challenge.

You can have multiple constructors. You can trivially replace your initialize() method with a constructor:
class Entity {  public Entity() {    // Default constructor...    }  // Instead of intialize(int,string,int)...  public Entity(int a, string b, int c ) {    // Do whatever initialize() used to do.  } }

Then you can do entity = new Entity(1,"Jim",1) and so on.

Quote:
P.S. Why are we not supposed to mark questions as SOLVED?

Because you don't own the thread, answers might be wrong, there might be follow-on information (as there is in this thread) et cetera.

It basically is a waste of everybody's time to do so.
Quote:Original post by jpetrie
Quote:
The initialize method is meant to be called by the main program to generate a specific kind of entity, when one is needed, since there are dozens of types within the class. The default constructor only generates a 'starting' entity. Initialize generates a 'mature' entity.

That's why I did it that way. But I'll try to think of a way to do it via the constructor. It'll be an interesting challenge.

You can have multiple constructors. You can trivially replace your initialize() method with a constructor:
class Entity {  public Entity() {    // Default constructor...    }  // Instead of intialize(int,string,int)...  public Entity(int a, string b, int c ) {    // Do whatever initialize() used to do.  } }

Then you can do entity = new Entity(1,"Jim",1) and so on.

Quote:
P.S. Why are we not supposed to mark questions as SOLVED?

Because you don't own the thread, answers might be wrong, there might be follow-on information (as there is in this thread) et cetera.

It basically is a waste of everybody's time to do so.


Ahh ok. Thanks for the explanation.

And I'm definitely going to try putting 'initialize' in the constructor. I can still have a setting ('type = 0') within the constructor that generates a 'starting' entity and the other type settings can generate a 'mature' entity. Neat!
You're missing the point. You don't need to pass anything to the constructor to tell it whether to construct a "default" or "mature" Entity; you just use the different constructors. On the other hand, you should consider whether these concepts should really be represented by different types.

Also, please remember that array indices start with 0.

This topic is closed to new replies.

Advertisement