正在更改另一个类C#中的实例变量

本文关键字:实例 变量 另一个 | 更新日期: 2023-09-27 18:24:11

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

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

主类

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

}

正在更改另一个类C#中的实例变量

也许当您构建Prices表单Prices=new Prices();时;,您可以将父对象的实例传递给子对象的构造函数。(例如,此关键字)。然后您将可以从子窗体访问父窗体的公共成员。例如

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

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

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

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 = //
    }
}