Ive created a console program that works. The program launches and asks for the user to input values for the player class.
Then I initialize a list of Monster classes and have values entered into the method to create each monster. But I am receiving an error after the program runs claiming the index value may be overloaded. w/e that means.

List<Monster> monsterlist = new List<Monster>();
            
monsterlist[0].CreateMonster("Goblin", 4, 4, 4, 1);
monsterlist[1].CreateMonster("Wolf", 2, 2, 2, 1);

This is my list and the values used. I run it and then...

"ArgumentOutOfRangeException was handled
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index"

pops up highlighting the line.. monsterlist[0].CreateMonster("Goblin", 4, 4, 4, 1); -- Thats the problem. I have no idea what im doing wrong even after reading the help files. I will conclude with the code of the Monster class. Thanks for the help!

public class Monster
    {
        private string Name = "Monstername";
        private int Damage = 1;
        private int Delay = 1;
        private int Atk = 1;
        private int Treasure = 0;


        public Monster()
        {

        }

        public void CreateMonster(string name, int damage, int delay, int atk, int treasure)
        {
            SetName(name);
            this.Damage = damage;
            this.Delay = delay;
            this.Atk = atk;
            this.Treasure = treasure;

        }

        public void SetName(string n)
        {
            this.Name = n;
        }

    }

the files are in the same namespace.

Recommended Answers

All 6 Replies

You have defined a List containing type Monster, but you have not created any items for the list using the "new" operator. Thus, any attempt to access an element of the array (List) will cause an error.

Try using the Add(Monster) method to add the items to your list, then define monster:

monsterList.Add(new Monster());
monsterList[0].CreateMonster(...);

CreateMonster method is an instance method. Create an instance of CreateMonster and then after you can add it to the list.

List<Monster> monsterlist = new List<Monster>();
        monsterlist.Add(new Monster());
        monsterlist.Add(new Monster());
        monsterlist[0].CreateMonster("Goblin", 4, 4, 4, 1);
        monsterlist[1].CreateMonster("Wolf", 2, 2, 2, 1);

That worked wonders for my lists guys! Thank you so so much.

lol problem is I have ran into another problem...

Error after running: "Input string was not in a correct format."

here is my code.. its using .Parse.

Console.WriteLine("Choose a Monster");
                Console.WriteLine("----------------");
                Console.WriteLine("1) Goblin");
                Console.WriteLine("2) Wolf");
                Console.WriteLine("3) Exit");
                Console.Write("Choice:");

                string choice = Console.ReadLine();


                
                switch(int.Parse(choice))
                {
                    case 1:

"switch(int.Parse(choice))" is what is highlighted.

I have the rest of the code completed(switch with cases and breaks) but the error occurs at the Parse.

again, ive compared to so many different lines of code in other programs and I have no idea what im doing wrong.

int ch;
        int.TryParse(choice, out ch);
        switch (ch)
        {
         .....
        }

Thanks Adatapost.

Im away from my home pc so I will try that when I get there. Can you explain Parsing a bit? Your code seems like the perfect fix but everything ive read on Parse doesnt explain what you have typed here.

Every Parse ive seen has one variable in it and doesnt require an 'out'. I assume the statement is sending the value of choice into the variable ch... but why is this neccessary. Wouldnt the value of choice be an int and able to be used for the switch?

Im such a novice at programming and this site seems perfect for learning from experts.

A parameter declared with out modifier is an output or reference parameter. An output (reference) parameter does not create a new storage location. Instead, an output parameter represents the same storage location as the variable given as the argument in the function invocation. Thus, the value of an output parameter is always same as the underlying variable.

class Sample{
   static void calc(int a,int b,out int add,out int sub, out int mul) {
      add=a+b; 
      sub=a-b;
      mul=a*b;
   }
  static void Main() {
    int a=19,b=30,p,q,r;
    calc(a,b,out p,out q,out r);
    Console.WriteLine("Addition {0}, Sub {1}, and Mul {2} ",p,q,r);
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.