-1

I have an issue with my C# code - somehow I cant get my switch to work, (ex case 1: Charmander(); break;) says "An object reference is required for the non-static field, method, or property "Program.Charmander()" - But I cant seem to figure out why it isnt getting referenced.

class Program
    {

        string[] PokemonList = 
            // Entries i vores array af tilfældige Pokémon
            { "Charmander", "Squirtle", "Bulbasaur", "Pikachu", "Eevee", "Gastly", "Jigglypuff" };

        int index = 0;

        static void Main(string[] args)
        {
            // array "køn"
            string[] gen = { "♂", "♀" };
            // Oprettelse af vores array "PokemonList"
            string[] PokemonList = 
            // Entries i vores array af tilfældige Pokémon
            { "Charmander", "Squirtle", "Bulbasaur", "Pikachu", "Eevee", "Gastly", "Jigglypuff" };
            foreach (string PokemonSelect in PokemonList)

                // Prints the name of all selectable Pokémon
                Console.WriteLine(PokemonSelect);
            Console.WriteLine("Which Pokémon will you choose?");


            //Chooses a random Pokémon from the string PokemonList
            Random RandomPokemon = new Random();
            int index = RandomPokemon.Next(PokemonList.Length);
            Console.WriteLine($"Your opponent is {PokemonList[index]}");


            //Menu over choices the user can press
            string StringMenu = Console.ReadLine();
            int NextChoice = Convert.ToInt32(StringMenu);


            switch (NextChoice)
            {
                case 1:
                    Charmander();
                    break;
                case 2:
                    Squirtle();
                    break;
                case 3:
                    Bulbasaur();
                    break;
                case 4:
                    Pikachu();
                    break;
                case 5:
                    Eevee();
                    break;
                case 6:
                    Gastly();
                    break;
                case 7:
                    Jigglypuff();
                    break;
            }

            Console.WriteLine("Your Pokémon's gender is: " + gen[new Random().Next(0, gen.Length)]);
        }

        public string Charmander()
        {
            Console.WriteLine("You choose: Charmander");
            return PokemonList[index];
        }

        public string Squirtle()
        {
            Console.WriteLine("You choose: {1}");
            return PokemonList[index];
        }

        public string Bulbasaur()
        {
            Console.WriteLine("You choose: {2}");
            return PokemonList[index];
        }

        public string Pikachu()
        {
            Console.WriteLine("You choose: {3}");
            return PokemonList[index];
        }

        public string Eevee()
        {
            Console.WriteLine("You choose: {4}");
            return PokemonList[index];
        }

        public string Gastly()
        {
            Console.WriteLine("You choose: {5}");
            return PokemonList[index];
        }

        public string Jigglypuff()
        {
            Console.WriteLine("You choose: {6}");
            return PokemonList[index];
        }

    }
}

...........................................................................

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214

1 Answers1

1

Your Main method is static (so: not associated with any object instance); you are trying to call the instance methods Charmander etc.

On which instance of Program do you mean? You don't actually create any instances. So: in this case, those methods could perhaps also be static. Alternatively, you'd need to create an instance somewhere, and use that. Since you have a mutable field (index), this is probably the better approach. For example:

static void Main(string[] args)
{
    new Program().DoTheThing();
}
void DoTheThing() // naming is hard
{
    // array "køn"
    string[] gen = { "♂", "♀" };
    // Oprettelse af vores array "PokemonList"
...

I'd probably also move the logic here out of the Program class, and just have that do the actual launch / args handling, so:

static void Main(string[] args)
{
    new PokemonWhatever().DoTheThing();
}

and move DoTheThing to class PokemonWhatever (and make it public or internal)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900