我有一个实验课。我创建了这个类的一些实例,并用这些对象填充组合框。我使用了DisplayMember和ValueMember属性。人口还可以,但是当我从组合框中读取selectedValue时,它会给我NullReferenceException。
这是我的代码:
public ref class ABC
{
ABC( Experiment^ exp )
{
this->exp = exp;
this->name = this->exp->getName();
}
property Experiment^ Exp
{
Experiment^ get()
{
return this->exp;
}
}
property String^ Name
{
String^ get()
{
return this->name;
}
}
Experiment^ exp;
String^ name;
}
Experiment^ e1;
this->combobox->Items(gcnew ABC(e1));
this->combobox->DisplayMember = "Name";
this->combobox->ValueMember = "Exp";
this->combobox->SelectedIndex = 0;
Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue); // nullReferenceException
答案 0 :(得分:1)
我不知道为什么,但是当我交换以下内容时
Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue);
这一行
Experiment^ e2 = ((ABC^)(this->combobox->SelectedItem))->Exp;
没关系,这解决了这个问题。