方法 '' 没有重载需要 1 个参数

No overload for method '' takes 1 argument

好吧,我这里的问题基本上就是标题中所说的。我正在尝试将我的 Player2 class 的 bool 值称为我们在学校的 Tic Tac Toe-project。我认为值得一提的是,我在 Form1.cs 的开头使用 "Player Player1, Player2;" 来创建我的 class、Player 的两个实例。我已经在互联网上阅读了多篇关于此的文章,但所有这些都是人们试图调用比他们提供的参数更多的参数。我看不出 true 或 false 的 bool 值如何大于 1。

提前致谢。

One of my buttons where this problem appears.

public void Btn1_Click(object sender, EventArgs e) >{

        {
            if (click1 == 0) 
            {
            if (Player2.GetActive(true))//(turn == 0)
                {
                 Btn1.Text = "X";
                }
                else
                {
                    >Btn1.Text = "O";
                }
                //turn++;
                click1++;
            }
            else
            {
                Btn1.Text = Btn1.Text;
           }
            display();
            checkit();
       }
    }

This is my player class.

` public class Player
{
    //Characteristics
    string name;
    int points;
    bool Active;

    //Constructor
    public Player() { points = 0; Active = true; }

    //Methods
    public void SetName(string n) { name = n; }
    public string GetName() { return name; }
    public void SetPoints(int p) { points = p; }
    public int GetPoints() { return points; }
    public void SetActive(bool a) { Active = a; }
    public bool GetActive() { return Active; }`

您有代码:

Player2.GetActive(true)

但是你定义get active为

public bool GetActive() { return Active; }`

所以您没有定义带参数的 GetActive 是正确的。

这里,

if(Player2.GetActive(true))

您正在向方法 GetActive 传递一个额外的参数 (true)。我们从[=15=的方法声明中可以看出,它不带参数:

public bool GetActive() { return Active; }
//                   ↑
//              empty parentheses

我想你的意思是 "if Player2.GetActive() is true..." 对吧?如果是true,则不需要指定你想要的值,只需这样做就可以:

if (Player2.GetActive())

如果要检查是否为假,在调用前加上!来否定结果:

if (!Player2.GetActive())

如 BugFinder 所说,您使用的是获取 Active-Value 的方法,而不是使用设置值的方法。

所以改变

Player2.GetActive(true)

Player2.SetActive(true)

作为补充:
由于我们在这里使用 C# 并且您的许多方法都称为 set 和 get,我建议您将这些方法更改为属性:

public string Name 
{
  get { return name; }
  set { name = value; }
}

public int Points 
{
  get { return points; }
  set { points = value; }
}

public bool Active 
{
  get { return active; }
  set { active = value; }
}

现在当您想要设置值时,您可以键入

Player2.IsActive = true;

并检查值简单类型代码,如

If(Player2.IsActive)
  //Do Stuff