Get an object from a collection of mixed type

Hi, I am new to OOP and I need help with a little problem. I used a collection called Monsters to store 3 types of objects. Spiders, Farmers, Gollum (irrelevant). My collection is an indexer, but when I use it to get an object from a collection, the object is non-profitable, but I really need TypeCast to have me the next opperation.

private void Form1_Load(object sender, EventArgs e) { CurrentOpponent Opponent = new CurrentOpponent(); Gollum myGollum = new Gollum(); AngryFarmer myFarmer = new AngryFarmer(); Ugly_Spider mySpider = new Ugly_Spider(); myMonsters.AddGollum(myGollum); myMonsters.AddFarmer(myFarmer); myMonsters.AddUgly(mySpider); progressBar1.Increment(100); progressBar2.Increment(100); Monster myCurrentOpponent = Opponent.randomEncounter(); //textBox1.Text = (this is where i need the type for a cast)myCurrentOpponent.name } 

Here is a random element in which I retrieve an object

  class CurrentOpponent { public Monster randomEncounter() { Random _random = new Random(); int opp = _random.Next(4); return myMonsters[opp]; } 

And finally, the index that returns the monster (the parent type of all three types of monsters)

  public Monster this[int xxx] { get { return (Monster)List[xxx]; } } 

Help would be really appreciated. !! thanks in advance

+4
source share
4 answers

Ideally, AngryFarmer , Ugly_Spider and Gollum should inherit from Monster :

 public class AngryFarmer : Monster { // ... } // etc. 

Then you can simply use List<Monster> :

 myMonsters = new List<Monster>(); myMonsters.Add(new AngryFarmer()); // works because AngryFarmer is a kind of Monster 

This will allow you to use polymorphism.

+4
source

you need to use interfaces ...... IMonster ..... IMonster then has a name

then make all your monsters implement IMonster

and just have a list of IMonsters

0
source

you can try using interfaces! take a look ...

 public interface IMonster { String Name { get; } Int32 Health { get; set; } } public class Spider : IMonster { public Spider() { _health = 100; } public string Name { get { return "Spider"; } } private int _health; public int Health { get { return _health; } set { _health = value; } } } public class Gollum : IMonster { public Gollum() { _health = 250; } public string Name { get { return "Gollum"; } } private int _health; public int Health { get { return _health; } set { _health = value; } } } class Program { static void Main(string[] args) { List<IMonster> monsters = new List<IMonster>() { new Gollum(), new Spider() }; IMonster randomMonster = GetRandomMonster(monsters); Console.WriteLine(randomMonster.Name + "/" + randomMonster.Health); } private static IMonster GetRandomMonster(List<IMonster> monsters) { //Your code for getting a random monster goes here! throw new NotImplementedException(); } } 

I really like this approach ... Imagine that you have an element in your game that is originally not exactly a monster. Say that this is a random element in your game, which after a certain event becomes a monster that your Hero must fight with (say, a game similar to the mighty and magical heroes). If you decided to add this function a long time after creating the game, it would become harmful / difficult / risky to change it, since this element could already inherit from another class. If you used interfaces, you would simply implement it on this object, and it could quickly behave like any other IMonster in your game. This means that this random entity could be passed as a parameter to the Fight method (IHero hero, IMonster monster);

0
source

Ideally, AngryFarmer , Ugly_Spider and Gollum should inherit from Monster

I studied your problem as a problem in the Tetris game: 1 / You have monsters, how I have figures. 2 / Each type of monster has its own properties ("Health", "Magic Point", "...") and behavior (attack, run, spell spell ...), as blocks have properties (color, position, state , ..) and (go down, turn right, turn left, ...)

In the game scene, you want a random monster that has certain properties and behavior, for example, I want to randomly shape a Shape. If this is your problem, you can try my code:

 public abstract class CMonster { int _HP; int _MP; //..and something like this public int HP { get { return this._HP; } set { this._HP=value;} } public int MP { get { return this._MP; } set { this._MP = value; } } public abstract void Run(); public abstract void Attach(); public abstract void CastSpell(); } public class CUgly_Spider : CMonster { public CUgly_Spider() { this.MP = 100;//your value here this.HP = 100;//your value here } public override void Attach() { //your implemetation here } public override void Run() { //your implemetation here } public override void CastSpell() { //your implemetation here } } public class CGollum : CMonster { public CGollum() { this.MP = 100;//your value here this.HP = 100;//your value here } public override void Attach() { //your implemetation here } public override void Run() { //your implemetation here } public override void CastSpell() { //your implemetation here } } class Test { private void InitTheGame() { CMonster curMonster=null; Random rnd = new Random(); //sample random if ((rnd.Next() % 2) == 0) { curMonster = new CGollum(); } else { curMonster = new CUgly_Spider(); } curMonster.Run();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider curMonster.Attach();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider curMonster.CastSpell();//when (rnd.Next() % 2) == 0 then the Gollum is doing else the Ugly_Spider } } 

I hope this can help you.

0
source

Source: https://habr.com/ru/post/1382806/


All Articles