【问题标题】:Changing instance variables from another class C#从另一个类 C# 更改实例变量
【发布时间】:2014-11-19 09:02:11
【问题描述】:

我有一个“主要”表格和一个“价格”表格。 Main 类有一个 Ingredient 子类,该子类的实例在 Main 构造函数中初始化。按下按钮后,主窗体启动价格窗体。价格表单有几个文本框和一个按钮。

问题是我想通过使用价格表单中的新输入数据来修改成分子类实例的变量。我意识到可以通过构造函数参数轻松地将这些变量传递给 Price 表单,但是,我不知道如何将这些修改后的变量传递回 Main 类,因为在我将需要它们之后关闭价格表格。

主类

public partial class Main : Form
{
    public class Ingredient
    {
        public string name;
        public  int weight;
        public  int price;
        public  int energy;
        public  int tmp;

        public Ingredient(string mName, int mWeight, int mPrice, int mEnergy)
        {

            name = mName;
            weight = mWeight;
            price = mPrice;
            energy = mEnergy;
        }
    }


    public Main()
    {
        InitializeComponent();
        Ingredient hazulnuts = new Ingredient("hazulnuts", 0, 0, 0);        
    }


    private void bEditPrices_Click(object sender, EventArgs e)
    {
        // I could pass variables of the Ingredients instance here through the constructor,
        // but haven't done so yet because I'm hoping there is some way that I can directly
        // access variables of instances of Ingredient class as there could be quite a lot 
        //of these instances
        Prices prices = new Prices();
        prices.Show();
        // The Main form is hidden when the Prices form is shown, therefore instantiating a new 
        //Main from the Prices isn't an option
        this.Hide();
    }

}

价格等级:

public partial class Prices : Form
{

    public Prices()
    {
        InitializeComponent();

    }

    private void save_Click(object sender, EventArgs e)
    {
    // retrieve values of textboxes somehow pass the variables back to the
    // Main form's Ingredient instances

     //I realize that the code below is not syntactically correct but I'm looking 
     //for something along of the lines of that.
        this.Close;
        Main.show();
    }


}

【问题讨论】:

  • 也许当您构建价格表单Prices prices = new Prices(); 时,您可以将父实例传递给子构造函数。 (例如 this 关键字)。然后,您将可以从子窗体访问父级的公共成员。
  • 您如何存储这些信息?我假设您将此数据存储到数据库或文件或其他东西?如果是这样,您的“价格”表单应该在表单关闭时将数据保存在某处,而不是您的 Ingredient 类应该只有一个名为 RefreshPrices() 的方法,并且该方法应该从数据库或其他任何地方加载新价格。

标签: c# winforms forms class instance-variables


【解决方案1】:

将 Main 类中的 Ingredient 实例定义为 public,并将 Main 类的引用传递给 Price。

public partial class Main : Form
{
    public Ingredient Hazulnuts { get; set; }


    private void bEditPrices_Click(object sender, EventArgs e)
    {
        Prices prices = new Prices(this);
        prices.Show();
        this.Hide();
    }
}


public partial class Prices : Form
{
    private Main _mainForm;
    private Prices()
    {
        InitializeComponent();

    }

    public Prices(Main mainForm) : Prices()
    {
        _mainForm = mainForm;

    }

    private void save_Click(object sender, EventArgs e)
    {
        _mainform.Hazulnuts = //

    }
}

【讨论】:

    【解决方案2】:

    也许当您从价格价格 = new Price(); 中构造价格时,您可以将父实例传递给子构造函数。 (例如这个关键字)。然后,您将可以从子窗体访问父级的公共成员。例如

    public partial class Prices : Form
    {
        private Main _parent;
    
        public Prices(Main parent)
        {
            this._parent = parent;
            InitializeComponent();
    
        }
    
        private void save_Click(object sender, EventArgs e)
        {
            this._parent.Name = "HelloWorld";
        }
    }
    

    然后从 Main 你将价格表单构造为:

    Prices prices = new Prices(this);
    

    请注意此处的控制模型,并尝试在整个应用程序中保持一致

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 2020-08-24
      • 2012-01-23
      • 2012-05-21
      • 2013-09-25
      • 2011-07-26
      • 1970-01-01
      相关资源
      最近更新 更多