2010-11-21 2 views
2
그것은 For 루프의 중간에 ArgumentOutOfRangeException을 던지고

, 내가 for 루프의 나머지 부분을 잘라 유의하시기 바랍니다ArgumentOutOfRangeException이

for (int i = 0; i < CurrentUser.Course_ID.Count - 1; i++) 
{  
    CurrentUser.Course[i].Course_ID = CurrentUser.Course_ID[i]; 
} 

과정에 대한 코드

public class Course 
{ 
    public string Name; 
    public int Grade; 
    public string Course_ID; 
    public List<string> Direct_Assoc; 
    public List<string> InDirect_Assoc; 
    public string Teacher_ID; 
    public string STUTeacher_ID; 
    public string Type; 
    public string Curent_Unit; 
    public string Period; 
    public string Room_Number; 
    public List<Unit> Units = new List<Unit>(); 
} 

입니다 및 CurrentUser (사용자의 새 선언)

public class User 
{ 
    public string Username; 
    public string Password; 
    public string FirstName; 
    public string LastName; 
    public string Email_Address; 
    public string User_Type; 
    public List<string> Course_ID = new List<string>(); 
    public List<Course> Course = new List<Course>(); 
} 

나는 정말로 노골적으로 내가 뭘 잘못하고 있는지 혼란스러워. 어떤 도움이라도 대단히 감사 할 것입니다.

+0

아마도 List는 비어 있습니다. 어디에서 코드를 초기화하고 값을 추가합니까? –

답변

9

오프셋이 존재하지 않으면 목록에 색인을 생성 할 수 없습니다. 예를 들어 빈 목록을 색인화하면 항상 예외가 발생합니다. 반면에

var list = new List<string>(); 
list[0] = "foo"; // Runtime error -- the index 0 doesn't exist. 

: 등 예를 들어

어딘가에 목록의 중간에 항목을 배치 할 목록 또는 Insert의 끝에 항목을 추가 할 Add 같은 방법을 사용합니다 : 당신은 Courses 목록에 기록 때, 당신은 Course_ID 목록에서 읽을 때 코드에서 이런 일이

var list = new List<string>(); 
list.Add("foo");  // Ok. The list is now { "foo" }. 
list.Insert(0, "bar"); // Ok. The list is now { "bar", "foo" }. 
list[1] = "baz";  // Ok. The list is now { "bar", "baz" }. 
list[2] = "hello";  // Runtime error -- the index 2 doesn't exist. 

하는 것으로.