Cannot Assign to 'Money' Because It Is a 'Method Group'

僤鯓⒐⒋嵵緔 提交于 2019-12-12 15:09:01

问题


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

回答1:


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



回答2:


It should be

money += 400;

or change the Method to a property,

public int Money {get;set;}

Depending on the context of where you are trying to do the increment (in class or in a class that uses the Money property (field)



来源:https://stackoverflow.com/questions/26164043/cannot-assign-to-money-because-it-is-a-method-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!