2011-12-05 52 views
1

我有一個實驗類。我創建了這個類的一些實例,並用這些對象填充組合框。我使用了DisplayMember和ValueMember屬性。人口是好的,但是當我從組合框中讀取selectedValue時,它給了我NullReferenceException。.net C++/CLI combobox valueMember 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 
+1

實際可編譯的郵政編碼。將未初始化的實驗對象傳遞給ABC構造函數是不明智的。否則,我看不到有什麼顯而易見的方式來重複例外。 –

+0

'ComboBox :: Items'是一個屬性,而不是一個方法;我同意漢斯的觀點,代碼是虛假的。 – ildjarn

回答

1

我不知道爲什麼,但是當我交流以下行

Experiment^ e2 = (Experiment^)(this->combobox->SelectedValue); 

這一行

Experiment^ e2 = ((ABC^)(this->combobox->SelectedItem))->Exp; 

這是好的,這解決了這個問題。

相關問題