C# using class object in multiple forms changes all variables within class
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 开发者_C百科= (frmWeeklyIndividualBudgets)frm;
if (tfrm.Program.ProgramCode == this.Program.ProgramCode)
{
frm.Focus();
return;
}
}
else
{
frm.Focus();
return;
}
}
}
form.MdiParent = this;
form.Show();
}
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 new Program
instead, when you create the form.
For example:
frmWeeklyIndividualBudgets tfrmWeeklyIndividualBudgets =
new frmWeeklyIndividualBudgets();
// Give the new form a new Program instance
tfrmWeeklyIndividualBudgets.Program = new Program();
tfrmWeeklyIndividualBudgets.Text = this.Program.ProgramCode.ToString()
+ " Weekly Budget";
this.CheckMdiChildren(tfrmWeeklyIndividualBudgets);
If you want the new form to get a Program
based on the existing one, you should implement a Clone
method in Program
, and do:
tfrmWeeklyIndividualBudgets.Program = this.Program.Clone();
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.
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:
tfrmWeeklyIndividualBudgets.Program = this.Program;
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.
tfrmWeeklyIndividualBudgets.Program = new Program { set properties here };
// Or
tfrmWeeklyIndividualBudgets.Program = this.Program.Clone();
精彩评论