简体   繁体   English

方法“”没有重载,接受1个参数

[英]No overload for method '' takes 1 argument

Well, my issue here is basically what it says in the title. 好吧,我的问题基本上就是标题中的内容。 I'm trying to call my bool value of my Player2 class for my Tic Tac Toe-project we have in school. 我正在为我在学校的Tic Tac Toe项目称呼我的Player2类的布尔值。 I think it's worth mentioning that I use "Player Player1, Player2;" 我认为值得一提的是,我使用的是“ Player Player1,Player2;” in the beginning of Form1.cs to create two instances of my class, Player. 在Form1.cs的开头创建我的类的两个实例Player。 I've read multiple posts on the internet about this but all of them are people trying to call in more parameters than that they are providing. 我已经在互联网上阅读了多个有关此内容的文章,但所有这些人都是试图调用比其提供的参数更多的人。 I don't see how a bool value of true or false is more than one. 我看不到布尔值true或false如何超过一个。

Thanks in advance. 提前致谢。

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

You have the code: 您有以下代码:

Player2.GetActive(true)

But you define get active as 但是你定义为

public bool GetActive() { return Active; }`

So it is correct you have not defined a GetActive with a parameter. 因此,您没有使用参数定义GetActive是正确的。

Here, 这里,

if(Player2.GetActive(true))

you are passing an extra argument ( true ) to the method GetActive . 您正在将一个额外的参数( true )传递给方法GetActive As we can see from the method declaration of GetActive , it takes no parameters: GetActive的方法声明中可以看到,它不带任何参数:

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

I think what you mean here is "if Player2.GetActive() is true..." right? 我认为您的意思是“如果Player2.GetActive()是真的...”对吗? You don't need to specify the value you want if it is true , just doing this is fine: 如果为true ,则无需指定所需的值,只需这样做就可以了:

if (Player2.GetActive())

If you want to check if it is false, add ! 如果要检查是否为假,请添加! before the call to negate the result: 在调用否定结果之前:

if (!Player2.GetActive())

Like BugFinder said, you are using the method to get the Active-Value instead of using the method to set the value. 就像BugFinder所说的那样,您使用的是方法来获取有效值,而不是使用方法来设置值。

So change 所以改变

Player2.GetActive(true)

to

Player2.SetActive(true)

Just as an addition: 作为补充:
Since we are working with C# here and many of your methods are called set and get, I suggest you change those methods to be properties: 由于我们在这里使用C#,并且您的许多方法都称为set and 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; }
}

Now when you want to set the value, you can type 现在,当您想要设置值时,您可以输入

Player2.IsActive = true;

and to check the value simple type code like 并检查值的简单类型代码,如

If(Player2.IsActive)
  //Do Stuff

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM