由于它是“方法组”,因此无法分配给“金钱”(Cannot Assign to 'Money' Because It Is a 'Method Group')

我正在为类做一个项目,我必须从我的Player类调用Money函数。 但是,我不知道如何将Money换成别的Method Group 。 我不懂编程,所以我的解决方案范围很小。 另外,教练说我不能对Main类做任何修改,只对Player类做任何修改。

以下是Main类的代码:

p1.Money += 400;

这里是我的Player类中的'Money'函数:

public int Money () { return money; }

I'm doing a project for class, and I have to call the Money function from my Player class. However, I do not know how to change Money into something else that is not a Method Group. I don't know much programming, so my range of solutions is rather small. Also, the instructor said I cannot make any changes to the Main class, only to the Player class.

Here's the code for the Main class:

p1.Money += 400;

And here's the 'Money' function from my Player class:

public int Money () { return money; }

最满意答案

Money()是一种方法。 你不能设置它 - 它只返回一些东西(一个int )(或者没有任何东西,如果void )。

您需要将其更改为也可以设置的属性 :

public int Money {get; set;}

或者更详细地说:

private int _money; public int Money { get { return _money; } set {_money = value;} }

Money() is a method. You can't set it - it only returns something (an int) (or nothing if void).

You need to change it to a property that can also be set:

public int Money {get; set;}

or, more elaborative:

private int _money; public int Money { get { return _money; } set {_money = value;} }由于它是“方法组”,因此无法分配给“金钱”(Cannot Assign to 'Money' Because It Is a 'Method Group')

我正在为类做一个项目,我必须从我的Player类调用Money函数。 但是,我不知道如何将Money换成别的Method Group 。 我不懂编程,所以我的解决方案范围很小。 另外,教练说我不能对Main类做任何修改,只对Player类做任何修改。

以下是Main类的代码:

p1.Money += 400;

这里是我的Player类中的'Money'函数:

public int Money () { return money; }

I'm doing a project for class, and I have to call the Money function from my Player class. However, I do not know how to change Money into something else that is not a Method Group. I don't know much programming, so my range of solutions is rather small. Also, the instructor said I cannot make any changes to the Main class, only to the Player class.

Here's the code for the Main class:

p1.Money += 400;

And here's the 'Money' function from my Player class:

public int Money () { return money; }

最满意答案

Money()是一种方法。 你不能设置它 - 它只返回一些东西(一个int )(或者没有任何东西,如果void )。

您需要将其更改为也可以设置的属性 :

public int Money {get; set;}

或者更详细地说:

private int _money; public int Money { get { return _money; } set {_money = value;} }

Money() is a method. You can't set it - it only returns something (an int) (or nothing if void).

You need to change it to a property that can also be set:

public int Money {get; set;}

or, more elaborative:

private int _money; public int Money { get { return _money; } set {_money = value;} }