Visual C # - Access instance of an object created in one class in another

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).

+7
source share
3 answers

You need to declare a Soldier instance in a higher region.

One way to do this is to declare it inside Form1, then pass it to Form2, etc.

 public class Form1 { private Soldier tempSoldier = new Soldier(); private void button1_Click(object sender, EventArgs e) { tempSoldier = new Soldier(); tempSoldier.surname = textbox1.text; } private void GotoNextStep() { // pass the existing instance to the next form Form2 form2 = new Form2(tempSoldier); // display form 2 ... } } 

Another possibility is to use a singleton variable in the class, to which all forms have access.

 public class MyAppManager { private static readonly Soldier _soldier = new Solider(); public static Soldier SoldierInstance { get { return _soldier; } } } private void button1_Click(object sender, EventArgs e) { MyAppManager.SoldierInstnace.surname = textbox1.text; } 

The first approach is approved if there is a clear sequence of forms; the latter is better if difference forms can be used at different times or revised.

+7
source

You must create a public property in your form that reveals the soldier. You can then access this property from other forms.

 // ... public Soldier Soldier { get; private set; } private void button1_Click(object sender, EventArgs e) { Soldier tempSoldier = new Soldier(); tempSoldier.surname = textbox1.Text; this.Soldier = tempSoldier; } // ... 

Your Form2 class might look something like this:

 public partial class Form2 { private Form1 form1; public Form2(Form1 form1) { this.form1 = form1; this.InitializeComponent(); } public void DoStuffWithForm1() { // ... string surname = this.form1.Soldier.surname; // ... } } 
+3
source

You can also make Soldier a static variable:

 public static Soldier soldier {get;set;} private void button1_Click(object sender, EventArgs e) { soldier = new Soldier(); soldier.surname = textbox1.text; } 

Code in other forms:

 Form1.soldier.name =""; 
0
source

All Articles