2011-12-05 3 views
1

실험 클래스가 있습니다. 이 클래스의 일부 인스턴스를 만들고 콤보 상자에 이러한 객체를 채 웁니다. DisplayMember 및 ValueMember 속성을 사용했습니다. 인구는 괜찮지 만 combobox에서 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'는 속성이 아니라 메서드입니다. 나는 Hans에 동의한다. 코드는 가짜 다. – ildjarn

답변

1

왜 그러나 나는이 라인에 다음 줄

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

를 교환 할 때

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

괜찮습니까?이 문제가 해결되었습니다.

관련 문제