Hi
I'm starting out with C# and am building a simple application to play around with C# code.
I have a main form (form1) in which I have declared and instantiated some Objects. Form1 then declares and shows a second form (form2). I want form2 to call methods and get and set properties in my declared objects from form1. ie to change their internal state so when I go back to form1 I can access the objects new internal values (with appropiate properties etc).
//example dice class
public class dice
{
private int currentSide;
public CurrentSide
{
get{return currentSide}
set{currentSide = value}
}
}
//Form1 class
public class Form1 : Form
{
public Dice D1;public
Form1(){
InitializeComponent();
D1 = new Dice();
//This line works okay as it is inside the same class as declared etc
D1.CurrentSide = 1;
Form2
secondForm = new Form2();secondForm.ShowDialog();
}
}
//Second Form Class
public class Form2 : Form
{
private int displaySide;
public Form2(){
InitializeComponent();
//This line won't compile with the error Form1 doesn't have D1
//Error 1 An object reference is required for the nonstatic field, method, or property 'Form1.D1'
displaySide = Form1.D1.CurrentSide;
}
}

Accessing Objects in Form1 from form2
Shane Poppleton
Thanks for that, 30seconds and I was away laughing. (on a problem that I've been stumped on for days!)
Problably not best practise but in this case I am happy for my second Form to know all about the first form, its just an extension really to make UI clearer and easier to follow.
First ever question to a forum, usually just let google search previous questions and answers, have made a questioner out of me, ~3hour turn around, MANY thanks.
My main reason is the objects have calculation methods with the bits of background logic. These methods returned the value that I then use the same objects Set properties to set, [if the user chooses that option]. (ie display all calculation results, user chooses, sets object back in Form1 and closes Form2, then when back in Form1 can access objects properties with get method.
I assume by setting public Form2 variables, form one would be able to access them between creating Form2 and showing it Then would need to assign back afterwards or does Form2 dissappear when closed
[code language="c#"]
Form2 theForm = new Form2(); //no "this" as "best practise" test
theForm.SomePublicForm2Property = "String text from Form1"
theForm.ShowDialog();
someString = theForm.SomePublicForm2Property
Showing my early learning C# skills now!
Cheers.
:-)
learningtoprogram22
basically to access objects etc... from one form to the other, you need to pass a reference of the Caller form to the calling form, example:
//MAIN FORM
private string someString = "";
private int someValue = 0;
public string TheSomeString
{
get { return this.someString; }
set { this.someString = value; }
}
public int SomeValue
{
get { return this.someValue; }
set { this.someValue = value; }
}
..
..
//Some method to show form 2:
Form2 theForm = new Form2(this);
theForm.ShowDialog(); //or Show() - whichever you like depending on what you want to do.
Form2:
private Form1 theMainForm = null;
public Form2(Form1 mainForm)
{
this.theMainForm = mainForm;
//And so on...
}
private SomeMethod()
{
//Set some values from the mainform
this.theMainForm.SomeString = "new value set from form2";
this.theMainForm.SomeValue = 10;
}
For best practice, you maybe better on declaring the same variables in form 2, exposing them publically (as we have done in form1) and setting those values from the caller (form1) to prevent form2 from dealing with other form1's functions (one form should not need to know about each other really, or at least accessing its methods which are not needed to be called for)....
does this help you at all
Doug DeBug
Here is some additional information:
First let us understand some basics:
(1) C# is case sensitive lanuage. So 'dice' and 'Dice' are two different variables.
(2) In Form1 class, dice would better be declared as a private variable.
(3) In property declaration, get and set statements should be terminated in a semicolon.
(4) If Form1 class instantiates a Form2 object and a Form2 class instantiates a Form1 object, stack overflow would result.
You may like to provide a button on Form1. The handler for click event of this button may be used to create an instance of Form2. Similarly a button on Form2 may be used to create an instance of Form1 class.
msdobrescu
no worries, glad I could help!
Well, when you set a property in Form2 and the form is later closed, it will get rid of any resources/variables used (disposing it) so itll get lost, so you would need to save whatever it is in form 2 into form1.
If you set variables in Form1 FROM Form2, you need not to worry as the variables will be accessible/set by form2 and still held in form1 since form1 is the caller form.