C# 以多种形式使用类对象会更改类中的所有变量
我有一个 MDI 应用程序。其中一种表单需要能够同时打开它的多个实例。在这个应用程序中,我有一个程序类。对于表单的每个实例,我需要将一个 Program 对象放入每个表单中。这是有效的,但是,每次数据更改时,它都会更改表单的所有多个实例中的所有程序对象。
这是 Program 类(目前非常简单的类):
public class Program
{
string strProgramCode;
public Program()
{ }
public string ProgramCode
{
get { return strProgramCode; }
set { strProgramCode = value; }
}
}
这是表单的代码:
frmWeeklyIndividualBudgets tfrmWeeklyIndividualBudgets = new frmWeeklyIndividualBudgets();
tfrmWeeklyIndividualBudgets.Program = this.Program;
tfrmWeeklyIndividualBudgets.Text = this.Program.ProgramCode.ToString() + " Weekly Budget";
this.CheckMdiChildren(tfrmWeeklyIndividualBudgets);
这是 CheckMdiChildren 方法:
private void CheckMdiChildren(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType())
{
if (frm.GetType().ToString() == "IPAMFinancial_Program_Financial_Breakdown.frmWeeklyIndividualBudgets")
{
frmWeeklyIndividualBudgets tfrm = (frmWeeklyIndividualBudgets)frm;
if (tfrm.Program.ProgramCode == this.Program.ProgramCode)
{
frm.Focus();
return;
}
}
else
{
frm.Focus();
return;
}
}
}
form.MdiParent = this;
form.Show();
}
I have a MDI application. One of the forms needs to be able to have multiple instances of it open at the same time. Within this app I have a Program class. For each instance of the form I need to place a Program object into each form. This is working, however, everytime data is changed it changes all of the Program objects within all of the multiple instances of the form.
Here is the Program class (very simple class for now):
public class Program
{
string strProgramCode;
public Program()
{ }
public string ProgramCode
{
get { return strProgramCode; }
set { strProgramCode = value; }
}
}
Here is the code for the form:
frmWeeklyIndividualBudgets tfrmWeeklyIndividualBudgets = new frmWeeklyIndividualBudgets();
tfrmWeeklyIndividualBudgets.Program = this.Program;
tfrmWeeklyIndividualBudgets.Text = this.Program.ProgramCode.ToString() + " Weekly Budget";
this.CheckMdiChildren(tfrmWeeklyIndividualBudgets);
Here is the CheckMdiChildren method:
private void CheckMdiChildren(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType())
{
if (frm.GetType().ToString() == "IPAMFinancial_Program_Financial_Breakdown.frmWeeklyIndividualBudgets")
{
frmWeeklyIndividualBudgets tfrm = (frmWeeklyIndividualBudgets)frm;
if (tfrm.Program.ProgramCode == this.Program.ProgramCode)
{
frm.Focus();
return;
}
}
else
{
frm.Focus();
return;
}
}
}
form.MdiParent = this;
form.Show();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我强烈怀疑问题在于您有一个
Program
对象,所有表单都引用该对象。 (这当然就是代码的样子。)在创建表单时,为每个表单指定一个newProgram
。例如:
如果您希望新表单获得一个
Program
基于现有的表单,您应该在中实现一个
,并执行以下操作:Clone
方法编程I strongly suspect the problem is that you've got one
Program
object, which all the forms refer to. (That's certainly what the code looks like.) Give each form a newProgram
instead, when you create the form.For example:
If you want the new form to get a
Program
based on the existing one, you should implement aClone
method inProgram
, and do:您应该创建一个 ProgramFactory 对象,并在每次需要时调用该对象上的某种类型的方法来创建一个新的 Program 对象。您似乎正在重复使用同一个实例。
You should create a ProgramFactory object, and call some type of method on that object to create a new Program object each time you need one. You appear to be reusing the same instance.
在.NET 中,对象通过引用传递(即,对对象的引用四处传递,而单个对象位于一个位置,由引用它的所有变量使用)。这意味着当您执行此操作时:
您有两个使用完全相同的对象的“程序”变量。
为了避免这种情况,您需要构造要分配的程序对象的新版本。有时,人们创建一个 Clone() 方法来处理这个问题。
In .NET, objects are passed by reference (i.e. references to the object are passed around, while the single object sits in one place, used by all the variables referring to it). This means that when you do this:
You have two "Program" variables using the exact same object.
To avoid this, you'll need to construct a new version of the program object to assign. Sometimes, people create a Clone() method to handle this.