2014-06-20 4 views
0

는 여전히 C#을 새로운 오전과 나는 항상 일을하려고 후, 그러나 나는 그것을 value cannot be null을 말한다 오류로 실행 수동 get 지금을 할 필요가 HERE하려는 SO 솔루션 당은 {get; set;} 선언과 멋 졌을 이 페이지의 "필터링 기준"HERE.서버 오류 - 값을 Null로 설정할 수 없습니다 - C#을 설정합니다.

set이 없기 때문에 오류가 내 디버거에서 여기를 가리 킵니다.

public string EmployeeNamesString 
    { 
     get { return string.Join(", ", this.employeeNames); } //System.ArgumentNullException 
    } 

은 내가 set { this.employeeNames = (someValue); } 같은 것을 시도해야한다 생각하지만 난 무엇으로 설정 .. 확실하지 않다

이 발생하는, 어떻게 내가이 문제를 해결할 수있는 이유 누군가가 나에게 설명해 주시겠습니까?

감사합니다.

처음에, 당신의 employeeNames 컬렉션 컬렉션이 초기화되어 있지 않은 경우 그래서 string.Join에 전화가 ArgumentNullException을 던질 것이다, null을 될 것입니다

public class StarringViewModel 
    { 
     public int movieID { get; set; } 
     public int roleID { get; set; } 
     public int employeeID { get; set; } 
     public string movieName { get; set; } 
     public string movieDescription { get; set; } 
     public DateTime? movieReleaseDate { get; set; } 
     public string Role { get; set; } 
     public string employeeName { get; set; } 
     public DateTime employeeBirthdate { get; set; } 
     public IEnumerable<string> employeeNames { get; set; } 
     public string EmployeeNamesString 
     { 
      get { return string.Join(", ", this.employeeNames); } 
      set { this.employeeNames = someValue; } //attempt 
     } 
    } 

답변

2

뷰 모델.

public IEnumerable<string> employeeNames { get; set; } 

public string EmployeeNamesString 
{ 
    get { return string.Join(", ", this.employeeNames); } 
} 

하나의 가능성은 생성자에서 employeeNames를 초기화하는 것입니다, 그래서 당신은 EmployeeNamesString에 액세스 할 때이 널 (null)이 아닙니다. 세터를 비공개로 설정하여 클래스 외부의 사람도 employeeNames을 null로 만들 수 없습니다.

public class StarringViewModel 
{ 
    public StarringViewModel 
    { 
     employeeNames = new List<string>(); 
    } 

    ... 
    ... 

    public IEnumerable<string> employeeNames { get; private set; } 

    public string EmployeeNamesString 
    { 
     get { return string.Join(", ", employeeNames); } 
    } 
} 
+0

대안, 직관적 일이기는하지만, 널 병합 연산자를 사용하는 것입니다 - ?? (string.Join를 (","돌아 {employeeNames를 얻을 수'(employeeNames = 새로운 목록 ())) }' –

+2

@DanielMann Eww ... 부작용을 숨기는 것은 우아하고 잠재적으로 혼란스럽고'employeeNames'가 설정되지 않았는지 확인하는 것과 같은 유용한 일을 할 수 없다는 것을 의미합니다. return employeeNames == null? null : string.Join (","employeeNames); ' – Corey

+0

@Corey 모든 계산에 동의했습니다! 그냥 던지십시오. –

관련 문제