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.
Asked
Active
Viewed 307 times
0
-
WebForms or WinForms? – Shay Mar 25 '14 at 22:13
3 Answers
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
-
Thanks for your response. I have 3 Forms, How can this be done to be accessed on the 3rd form? – user3461835 Mar 25 '14 at 22:22
-
-
I don't know what you mean by "twice on one form." If you want to open two other forms from a form, yes, you can do that. It might be confusing for the user, though. – Robert Harvey Mar 25 '14 at 22:26
-
I want to access the name the user entered (in a text box) on the start screen on 3 other forms – user3461835 Mar 25 '14 at 22:29
-
Each form can have its own constructor method, just like the one I described above. `MyOtherForm(string name);` `MyThirdForm(string name);` – Robert Harvey Mar 25 '14 at 22:30
-
I get this error message when trying to access the class - Object reference not set to an instance of an object. – user3461835 Mar 25 '14 at 22:44
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