C# 以多种形式使用类对象会更改类中的所有变量

发布于 2024-09-08 09:53:25 字数 1516 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-09-15 09:53:26

我强烈怀疑问题在于您有一个 Program 对象,所有表单都引用该对象。 (这当然就是代码的样子。)在创建表单时,为每个表单指定一个new Program

例如:

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

如果您希望新表单获得一个Program基于现有的表单,您应该在中实现一个Clone方法编程,并执行以下操作:

tfrmWeeklyIndividualBudgets.Program = this.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 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();
海风掠过北极光 2024-09-15 09:53:26

您应该创建一个 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.

未蓝澄海的烟 2024-09-15 09:53:26

在.NET 中,对象通过引用传递(即,对对象的引用四处传递,而单个对象位于一个位置,由引用它的所有变量使用)。这意味着当您执行此操作时:

tfrmWeeklyIndividualBudgets.Program = this.Program;

您有两个使用完全相同的对象的“程序”变量。

为了避免这种情况,您需要构造要分配的程序对象的新版本。有时,人们创建一个 Clone() 方法来处理这个问题。

tfrmWeeklyIndividualBudgets.Program = new Program { set properties here };

// Or

tfrmWeeklyIndividualBudgets.Program = this.Program.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:

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文