Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I apologize in advance with what will probably be a fairly easy/quick answer based on scope, but I've looked everywhere and am surprised to not come up with an answer.

I have created a class called Soldier with roughly 100 class variables. I need a user to enter information and gradually build a Solider object over the course of several different class Forms (because there is too much data to collect on just one).

I create an (empty) instance of a Soldier (tempSoldier) in Form1.cs and start to set the object's class variables that I collect from the user.

private void button1_Click(object sender, EventArgs e)
{
    Soldier tempSoldier = new Soldier();
    tempSoldier.surname = textbox1.text;
}

My question: how do I gain access to the object instance (tempSoldier) from Form1.cs in the other classes (Form2.cs, Form3.cs ...)?

I should mention that all of the Forms (Form1.cs, Form2.cs ...) share the same namespace.

Thanks in advance

Edit: All solutions below work so it just depends upon which one you like the best. Thank you for your feedback. One little note, make sure you make ALL of the class modifiers Public including your custom class (in my case Soldier.cs).

like image 838
M. Black Avatar asked Sep 09 '12 01:09

M. Black


People also ask

What is Visual C used for?

Microsoft Visual C++ is a integrated development environment (IDE) used to create Windows applications in the C, C++, and C++/CLI programming languages. It was originally a standalone product, but is now included as part of Microsoft Visual Studio.

Is Microsoft Visual C free?

A fully-featured, extensible, free IDE for creating modern applications for Android, iOS, Windows, as well as web applications and cloud services.

Is Visual C necessary?

We don't recommend that you delete any Visual C++ redistributable, because doing so could make multiple applications on your computer stop working. Given how little space they take up and how broadly they are used, it doesn't seem worth the hassle to mess with your current ecosystem of standard library files.

Is Visual Studio C or C++?

The Visual Studio IDE supports C++ development on Windows, Linux, Android and iOS with a code editor, debugger, test frameworks, static analyzers, and other programming tools.


2 Answers

You'll need to declare the Soldier instance in in a higher scope.

One way of doing this would be to declare it inside Form1, then pass it to Form2, and so on.

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 a class that all the forms have access to.

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 former approach is ok if there's a distinct sequence to the forms; the latter is better if difference forms could be used at different times or revisited.

like image 139
McGarnagle Avatar answered Sep 19 '22 13:09

McGarnagle


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 ="";
like image 30
Svexo Avatar answered Sep 22 '22 13:09

Svexo