Visual C# - 在另一个类中创建的对象的访问实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12335918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual C# - Access instance of object created in one class in another
提问by M. Black
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 Soldierwith roughly 100 class variables. I need a user to enter information and gradually build a Soliderobject over the course of several different class Forms (because there is too much data to collect on just one).
我创建了一个名为Soldier的类,其中包含大约 100 个类变量。我需要一个用户输入信息并在几个不同的类 Forms 的过程中逐渐构建一个Solider对象(因为有太多的数据需要收集一个)。
I create an (empty) instance of a Soldier (tempSoldier) in Form1.csand start to setthe object's class variables that I collect from the user.
我在Form1.cs 中创建了一个士兵 (tempSoldier) 的(空)实例,并开始设置我从用户那里收集的对象的类变量。
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 ...)?
我的问题:如何从其他类(Form2.cs、Form3.cs ...)中的 Form1.cs 访问对象实例(tempSoldier)?
I should mention that all of the Forms (Form1.cs, Form2.cs ...) share the same namespace.
我应该提到所有表单(Form1.cs、Form2.cs ...)共享相同的命名空间。
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).
编辑:以下所有解决方案都有效,因此这取决于您最喜欢哪一个。感谢您的反馈意见。一个小注意事项,确保您将所有类修饰符设为 Public,包括您的自定义类(在我的情况下为 Soldier.cs)。
采纳答案by McGarnagle
You'll need to declare the Soldierinstance in in a higher scope.
您需要Soldier在更高的范围内声明实例。
One way of doing this would be to declare it inside Form1, then pass it to Form2, and so on.
这样做的一种方法是在Form1 中声明它,然后将它传递给Form2,依此类推。
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.
如果表单有不同的顺序,前一种方法是可以的;如果不同的形式可以在不同的时间使用或重新审视,后者会更好。
回答by Dan
You should create a public property on your form that exposes the soldier. You can then access this property from the 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 Form2class could look something like this:
您的Form2课程可能如下所示:
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;
// ...
}
}
回答by Svexo
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 ="";

