3

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;
    }
hunch_hunch
  • 2,283
  • 1
  • 21
  • 26
FCain 1026
  • 59
  • 2
  • 6
  • `Money` is a method, it will return something. You need to call it like `p1.Money()` and then assign the result back. Something like `p1.SomeProperty = p1.Money() + 400;` – Habib Oct 02 '14 at 15:18
  • 1
    It sounds like you want a *property* or a *field*. You might need to start with some basics of C#. – crashmstr Oct 02 '14 at 15:19

2 Answers2

4

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;} }
David S.
  • 5,965
  • 2
  • 40
  • 77
  • You know, I started to problem solve going through my professor's lectures and code samples, and I came up with the same exact thing. Thank you so much! – FCain 1026 Oct 02 '14 at 15:41
3

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)

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164