【问题标题】:Passing an instance of a class as a parameter in C#在 C# 中将类的实例作为参数传递
【发布时间】:2017-08-26 15:51:32
【问题描述】:

所以,当按下一个键时,我需要将 Labyrinth 类的一个实例从 Form1 类传递给 Player 类,以便玩家能够在没有碰撞的迷宫中移动。 首先,我全局声明了一个 Labyrinth 对象,这样当在 OnMouseClick 函数中创建时,它将在类中的任何地方可用,然后在 OnKeyPress 函数中,我将它传递给 Player 类的实例,并使用 Move 函数执行移动并检查碰撞。
我得到“对象引用未设置为对象的实例”异常,我不知道为什么它不起作用。也许我在声明或实例化迷宫类时搞砸了,但我根本不知道其他方法。有什么建议吗?

namespace Labyrinth
{
    public partial class Form1 : Form
    {
        Player player = new Player();
        Labyrinth labyrinth;

        private void OnMouseClick(object sender, MouseEventArgs e)
        {
            if (window=="lvlSelect")
                string clicked = levelMenu.Click(e);

            switch (clicked)
            {
                case "1":
                  level = 1; 
                  Labyrinth labyrinth = new Labyrinth(level);
                  break;
                case "2":
                  level = 2; 
                  Labyrinth labyrinth = new Labyrinth(level);
                  break;
                case "3":
                  level = 3; 
                  Labyrinth labyrinth = new Labyrinth(level);
                  break;
            }           
        }

        private void OnKeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            if (window == "game")
            {player.Move(k = key.ToString(), labyrinth)}
        }
    }
}

namespace Labyrinth
{
    class Player
    {
        int playerPosX;
        int playerPosY;
        public void Move(string key, Labyrinth labyrinth)
        {
            switch (key)
            {
                case "a":
                    if (!(playerPosX - 1 < 0) && 
                        (labyrinth.CheckCollision(playerPosX - 1, playerPosY)==false)) //I get the exception here
                        playerPosX--;
                    break;
                case ...
            }
        }
    }
}

【问题讨论】:

  • Labyrinth labyrinth = new Labyrinth(level); 应该只是 labyrinth = new Labyrinth(level);
  • 你的问题可能是因为clicked不等于123,导致labyrinth没有被初始化。尝试将default 添加到您的switch 语句中。此外,您只需编写 Labyrinth labyrinth = new Labyrinth(int.Parse(clicked)); 即可完全消除 switch 语句
  • 感谢您的回答。如果我在 OnKeyPress 函数和 Move 函数中分别初始化 Labyrinth,它应该被初始化,因为它可以正常工作,所以 switch 可以按预期工作。我尝试全局声明 Labyrinth 并通过它以避免一些问题。
  • 也许你试试 player.Move(k = key.ToString(), this)
  • 它不起作用,因为我传递它的类是 Form1,我需要传递 Labyrinth 的实例,而不是 Form1

标签: c# class exception


【解决方案1】:
public partial class Form1 : Form
    {
        Player player = new Player();
        Labyrinth labyrinth = new Labyrinth(1); // you need to default it to 1 first instead of null. because you might key press then only mouse click

        private void OnMouseClick(object sender, MouseEventArgs e)
        {
         string clicked = "1"; /// default clicked to 1
            if (window=="lvlSelect") /// if selected level is diff, then change
                 clicked = levelMenu.Click(e);
        labyrinth = new Labyrinth(Convert.ToInt(level)); 
        }

        private void OnKeyPress(object sender, KeyPressEventArgs e)
        {
            char key = e.KeyChar;
            if (window == "game")
            {player.Move(k = key.ToString(), labyrinth)}
        }
    }

【讨论】:

  • 您可以改为移除开关(单击)。只需将 15 行更改为一行。 labyrinth = new Labyrinth(convert.toint(clicked));
  • 如果我将它从case中移除,我如何在函数外使用参数“level”声明它?
  • 我的错。您的代码在编译时应该有错误。让我更改现有代码
  • 我已更新代码,大写/小写字母或拼写可能有错误。请我暂时不要和我在一起。
  • 非常感谢。我会尝试并告诉你它是否有效。
猜你喜欢
  • 2013-12-17
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-03-29
  • 2015-04-12
  • 1970-01-01
  • 2012-11-12
相关资源
最近更新 更多