的Visual C# - 一类在另一个创建的对象的Access实例 [英] Visual C# - Access instance of object created in one class in another

查看:110
本文介绍了的Visual C# - 一类在另一个创建的对象的Access实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我事先什么将可能是基于范围相当容易/快速回答道歉,但我到处找和我惊讶地没有拿出答案。

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.

我创建了一个名为战士类约100类变量。我需要用户输入信息,并逐步建立孙中山军人对象在几个不同的类表格的过程中(因为有太多的数据收集只有一个)。

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

我创建一个士兵(tempSoldier)的(空)实例的 Form1.cs的并开始设置对象的类变量,我从收集用户。

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;
}



我的问题:我怎么获得从对象实例(tempSoldier)在其他类Form1.cs中(Form2.cs,Form3.cs ...)?

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

我应该指出,所有的表格(Form1.cs中,窗体2中。 。CS ...)共享同一命名空间

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

在此先感谢

编辑:下面所有的工作解决方案,使它只是取决于你喜欢哪一个是最好的。感谢您的反馈意见。
一个小纸条,确保你所有的类修饰符public包括您的自定义类(在我的情况Soldier.cs)的。

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

推荐答案

您需要在更高的范围申报战士实例英寸

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

这样做将是内声明它的一个方法的 Form1中的,然后将它传递给的窗体2 的等。在

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.

这篇关于的Visual C# - 一类在另一个创建的对象的Access实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆