-4

I've got a problem with the following code:

public class Player
{
    public int money;
    public void setMoney(int amount)
    {
        money = amount;
    }
}

public void init()
{
    Player Player1 = new Player();
}

public void main()
{
    Player1.money = 9001;
}

private void mainForm_Load(object sender, EventArgs e)
{
    init();
    main();
}

When I run the given code I get an error that Player1 was not available in that context. So I guess i can't create an Player object in init and then use it in main.

How can I solve that problem?

Regards, dncrft

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
dnc
  • 600
  • 1
  • 5
  • 13
  • Please don't post fake code. – David Heffernan Dec 06 '14 at 16:06
  • 1
    Player1 is a local variable. It is only accesible from withing the init() method. – Giorgos Betsos Dec 06 '14 at 16:06
  • Yeah, i realised that. But I can't add a "public" keyword or something like that :/ By the way, it is no fake code. Just a shorter and english translated version of my original one. – dnc Dec 06 '14 at 16:07
  • 1
    This code is trivially simple. Surely the real problem is more complex. In which case the correct solution for the real problem is likely different from the solution to this problem. – David Heffernan Dec 06 '14 at 16:07
  • `voic` is not a keyword. The code was fake. We have no confidence that this is your real code. – David Heffernan Dec 06 '14 at 16:08
  • Seriously need to learn OOPS concepts. Classes and objects are base of OOPS, so understand them – Chaturvedi Dewashish Dec 06 '14 at 16:08
  • Bro, i fixed the voic error 4 minutes ago. – dnc Dec 06 '14 at 16:09
  • 3
    Yes, but my point stands. You posted fake code. Which means that there may be other errors made when you typed out the code rather than pasted it from your editor. You don't have to listen to my advice. But I'm still going to offer it. Never post fake code. Always copy code directly from your editor so that we are sure to have the actual code. – David Heffernan Dec 06 '14 at 16:12

2 Answers2

2

you declare an instance of Player in init(), the variable scoping rules in C# define that you can not use a variable outside the scope it defined. so Player1 is only accessible in init(). If you want to use it outside the init() you should define it in a scope that is visible to main()

public class Player
{
 public int money;
 public void setMoney(int amount)
 {
  money = amount;
 }
}
Player Player1;
public void init()
{
 Player1 = new Player();
}

public void main()
{
 Player1.Money = 9001;
}

private void mainForm_Load(object sender, EventArgs e)
{
 init();
 main();
}

some problems in your code:
1. Player.money is field, if it has a setter try set it private and use a setter and getter or make it as a property, public int money { set; get; }
2. Consider naming conventions in C#
3. Consider using nested classes

so I suggest this code

public class Player
{
   public int Money { get; set; }
}

public MainForm : Form
{
  private Player player1;

  public void Init()
  {
     this.player1 = new Player();
  }

  public void Main()
  {
     this.player1.money = 9001;
  }

  private void mainForm_Load(object sender, EventArgs e)
  {
    Init();
    Main();
  }
}
Community
  • 1
  • 1
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • A naming convention would be `Pascal Case` for properties. As stated in http://msdn.microsoft.com/en-us/library/vstudio/ms229043%28v=vs.100%29.aspx. So, in his case, it would be `Money` – Luis Lavieri Dec 06 '14 at 16:16
  • I just changed OP's code, an edit is on it's way @LuisLavieri – Hamid Pourjam Dec 06 '14 at 16:17
  • Okey, this would be a solution - also the getter and setter are good ideas - I haven't written object orientated code too long. The scope thing lead me to the right direction, I guess I know another way better solution now.. will try it. – dnc Dec 06 '14 at 16:21
2

Your problem is that instance of Player is out of scope. Also, you may want to use the shorthand declaration for your property.

public class Player
{
    public int Money {get; set;}
}
public void main()
{
    Player player = new Player();
    player.Money = 9001;
}
Luis Lavieri
  • 4,064
  • 6
  • 39
  • 69