【问题标题】:What does the error “Object reference required” mean? [duplicate]“需要对象引用”错误是什么意思? [复制]
【发布时间】:2020-01-21 12:34:37
【问题描述】:

我的 C# 代码有问题 - 不知何故我无法让我的开关工作,(例如案例 1:Charmander(); break;)说“非静态字段、方法或属性“Program.Charmander()” - 但我似乎无法弄清楚为什么它没有被引用。

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];
        }

    }
}

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

【问题讨论】:

  • public static string Charmander()
  • 您还需要将PokemonListindex 设为静态。或者将它们传递给那些方法。
  • 将此方法设为静态 Charmander();松鼠();大头龙();皮卡丘();伊布();加斯利(); Jigglypuff();

标签: c# reference


【解决方案1】:

您的Main 方法是static(因此:不与任何对象实例关联);您正在尝试调用 instance 方法 Charmander 等。

您是指Program 的哪个实例?您实际上并没有创建 任何 实例。所以:在这种情况下,这些方法可能static。或者,您需要在某处创建一个实例,并使用 that。由于您有一个可变字段 (index),这可能是更好的方法。例如:

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

我可能还会将Program 类的逻辑移出,然后让它进行实际的启动/参数处理,所以:

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

并将DoTheThing 移动到class PokemonWhatever(并使其变为publicinternal

【讨论】:

    猜你喜欢
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    • 1970-01-01
    • 2016-03-28
    • 2015-07-05
    • 2016-03-24
    相关资源
    最近更新 更多