Sir Maxwell Sir Maxwell - 1 year ago 75
C# Question

Calling an array Item from another class

I got this bit of code here:

IEnemy[] Enemy = new IEnemy[2];
Enemy Goblin = new Enemy("Goblins", "Looks dirty and not so friendly", 100);
Enemy[0] = Goblin;

I can use it easily with this:

First I like One-Liner when possible, so is it possible to make it smaller?

Second, I created my enemy in the Main, but I always try to keep my main small and tidy, so I wanna use another class where i create these enemies and I can call them from my Main or any other class... I was thinking about Singleton, I'm just not so sure if it's the right thing to use or a static array... Any pointers?

Answer Source

Sounds like you need to use a parent class that has a list of other entities.

Something like:

var world = new World();
world.Enemies.Add(new Enemy{type="Goblin"});
world.Heros.Add(new Hero{name="Bart Simpson"});

World would need to have collections defined for each type, such as: Enemies, Heros, Vehicles, Treasure, etc...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download