从另一个类C#更改实例变量

时间:2014-09-24 18:44:46

标签: c# winforms forms class instance-variables

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

问题是我想通过使用价格表单中的新输入数据来修改Ingredient子类的实例的变量。我意识到将这些变量传递给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();
    }


}

2 个答案:

答案 0 :(得分:0)

也许当您构建价格表格价格=新价格();时,您可以将父项的实例传递给子项的构造函数。 (例如此关键字)。然后,您将可以从子表单访问父级的公共成员。例如

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

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

答案 1 :(得分:0)

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

    }
}