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();
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
source share