使用不同形式的对象
本文关键字:对象 | 更新日期: 2023-09-27 18:25:04
我需要帮助才能将对象从一种形式使用到另一种形式。这是我的代码:
形式1:
Refuge refuge = new Refuge("Iberville", chemin + imgIberville, 1, 35, 35);
this.Hide();
reservation_form.Show();
在表格2中(reservation_form):
labelNomRefuge.Text = // here I want to get the first parameter from the object
这是我的课:
public class Refuge
{
// propriétés de la classe
public string nomRefuge { get; set; }
public string chemImage { get; set; }
public int numRefuge { get; set; }
public int capacite { get; set; }
public int placesDispo { get; set; }
//constructeur avec paramètres
public Refuge(string pNomRefuge, string pChemImage, int pNumRefuge, int pCapacite, int pPlacesDispo)
{
nomRefuge = pNomRefuge;
chemImage = pChemImage;
numRefuge = pNumRefuge;
capacite = pCapacite;
placesDispo = pPlacesDispo;
}
但在形式2中,客体显然是不存在的。
这里有一种方法:
在表格2中添加此变量:
public Refuge refuge;
然后当你显示表单时:
reservation_form.refuge = this.refuge // assign a reference to the object
reservation_form.Show();
将房产添加到您的预订表格
class Reservation_Form
{
// ...
private Refuge _refuge;
public Refuge Refuge
{
get
{
return _refuge;
}
set
{
_refuge = value;
this.labelNomRefuge.Text = _refuge.RefugeName;
}
}
// ..
}
然后你可以这样称呼它:
Refuge refuge = new Refuge("Iberville", chemin + imgIberville, 1, 35, 35);
reservation_form.Refuge = refuge;
this.Hide();
reservation_form.Show();