I apologize in advance for what is likely to be a fairly easy / quick answer based on volume, but I looked everywhere and was surprised to not receive an answer.
I created a class called Soldier with approximately 100 class variables. I need the user to enter information and gradually create a Solider object over several different classes of forms (because there is too much data to collect on just one).
I create an (empty) instance of the Soldier (tempSoldier) in Form1.cs and start by setting the class variables of the objects that I collect from the user.
private void button1_Click(object sender, EventArgs e) { Soldier tempSoldier = new Soldier(); tempSoldier.surname = textbox1.text; }
My question is: how to access an object instance (tempSoldier) from Form1.cs in other classes (Form2.cs, Form3.cs ...)?
I should mention that all forms (Form1.cs, Form2.cs ...) use the same namespace.
Thanks in advance
Edit: all the solutions below work, so it depends on which one you like best. Thanks for your feedback. Another small note: make sure you include all modifiers of the Public class, including your own class (in my case, Soldier.cs).
M. Black
source share