0

I have a value in my first form of a game which I would like to access on the next 2 forms. This value is saved in a private class. How would I go about doing this? All help is greatly appreciated.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

3 Answers3

0

Create a new constructor in your target Winform class that takes the value as a parameter. Do the same for the subsequent form.

private MyType _value;

public MyForm(MyType myValue) : this()
{
    _value = myValue;
}

To show the new form from your original form:

var form = new MyForm(someValue);
form.Show(); // or ShowDialog()
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
0

Use get/set to define your value:

Class Form1
int _x; // value you want to access from other class/form
public int YourNumber;
{
  get
  {
    return this._x;
  }
  set
  {
     this._x= value;
  }
}

Now to access/modify from other class for example Form2:

Class Form2
Form1 form = new Form1();
form.YourNumber = 5; // and it's changed in Form1 also

int y = form.YourNumber; // and you get it from Form1
chouaib
  • 2,763
  • 5
  • 20
  • 35
0

Use Global Variable concept so that you can access across multiple forms. You can find details from How to make global variables?

Example: http://dotnetmirror.com/Articles/wpf/116/global-variables-in-wpf-winforms-mvvm

Community
  • 1
  • 1
dotnetmirror.com
  • 293
  • 2
  • 10