【发布时间】:2011-12-05 12:05:14
【问题描述】:
我有一个实验课。我创建了这个类的一些实例并用这些对象填充组合框。我使用了 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
【问题讨论】:
-
实际可以编译的邮政编码。将未初始化的 Experiment 对象传递给 ABC 构造函数是不明智的。否则,我看不到任何明显的方法来重现异常。
-
ComboBox::Items是属性,不是方法;我同意 Hans,代码是假的。
标签: .net combobox c++-cli nullreferenceexception