-1

I currently have two forms; Form1 and Form2. Form1 is used for my clickergame, where I have a double called "money". I need to access this value from Form2. I've set the double "money" public, and I have called form1 with:

Form1 mainform = new WindowsFormsApplication3.Form1();

Even though I've done this, I do not receive the value when i do mainform.money What could I possibly be doing wrong?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Gustavo A.
  • 25
  • 7

2 Answers2

0

Exposing a variable from one form to the rest of the world is not a good practice. You should encapsulate/hide the variable in a property with read only access or a method, pass the value as constructor parameter, etc.. Samples bellow use injection constructor a read-only property access.

Method1 Using Constructor Injection

// Form1
private double _money = 0;
_money = 100;
Form2 frm2 = new Form2(money);

//Form2
private double _money = 0;
public Form2(double money )
{
    _money = money;
}

Method2 Using read-only getter access

// Form1
public double Money {get;private set;}

//Form2
Form1 frm1 = new Form1();
private double _money = 0;
_money = frm1.Money;
E-Bat
  • 4,792
  • 1
  • 33
  • 58
-1

I recommend you to write the variable money at the two forms:

// Form1
private Form2 frm2 = new Form2();
public double money = 0;

private void changeMoney()
{
    frm2.money = /* value */;
    this.money = /* value */;
}
// Use this method to change money from form1


// Form2
private Form1 frm1 = new Form1();
public double money = 0;

private void changeMoney()
{
    frm1.money = /* value */;
    this.money = /* value */;
}
// Use this method to change money from form1

Another option is to create a static class like this:

public static class values
{
    public static double money = 0;
}

// From the form1 or form2, you can change the money like this:
values.money = /* value */; // set money
var x = values.money; // get money

I hope this answer helped you, Gerry

gerardet46
  • 76
  • 1
  • 9
  • Repeat code like your first example is generally bad and should be avoided. – Rasmus Søborg Apr 13 '17 at 15:36
  • Second option worked out fine, thanks! – Gustavo A. Apr 13 '17 at 15:40
  • Recursively creating instances like that is a bad idea... As for static fields, they're essentially global state. That often results in code that's difficult to maintain due to having too many (invisible) dependencies. A much better solution is to pass dependencies around explicitly: if `Form2` needs to display some data, pass that data via its constructor. In this case, you could pass it a `Form1` reference, but it's better to separate the UI from the actual data and logic you're working with, so a `GameState` class that can be used by both forms would be better still. – Pieter Witvoet Apr 13 '17 at 15:42
  • Exposing public variables from types is plain wrong. Very bad answer. – E-Bat Apr 13 '17 at 15:49
  • While this may work for the person asking the question, I would urge them try to understand why this answer is bad, and learn how to properly pass data using references. – Bradley Uffner Apr 13 '17 at 16:31