日期:2014-05-18  浏览次数:20849 次

这段代码哪里错了?问什么会返回-1?
/*
 * Created by SharpDevelop.
 * User: geekuser
 * Date: 25/6/2012
 * Time: PM 4:36
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
class Course
{
private string m_Name;
private int m_CourseID;
private int m_Score;

public Course()
{

}
public Course(string name,int courseID, int score)
{
name=m_Name;
courseID=m_CourseID;
score=m_Score;
}

public string Name
{
get{return m_Name;}
set{m_Name=value;}
}

public int CourseID
{
get{return m_CourseID;}
set{m_CourseID=value;}
}

public int Score
{
get{return m_Score;}
set{m_Score=value;}
}
}

class CourseIndexer//用于存储和查询Course类
{
private ArrayList arrCourse;
public CourseIndexer()//无参构造器,用来创建ArrayList
{
arrCourse=new ArrayList();
}

public int this[string name,int CourseID]
{
get
{
foreach(Course cs in arrCourse)
{
if (cs.Name==name&&cs.CourseID==CourseID)//name和CourseID是输入参数,跟cs.Name,cs.CourseID作对比,如果相同,就找到了相应的分数
{
return cs.Score;
}
}
return -1;
}

set
{
arrCourse.Add(new Course(name,CourseID,value));//arr["Leon",1]=90
}
}


}
class Test
{
static void Main()
{
CourseIndexer csi=new CourseIndexer();
csi["Leon",1]=90;
csi["Leo",2]=80;
csi["Le",3]=70;

Console.WriteLine(csi["Leo",2]);

}
}


为什么会返回-1,而不是输出80?谢谢

------解决方案--------------------
Course 的构造函数
public Course(string name, int courseID, int score)
{
name = m_Name;
courseID = m_CourseID;
score = m_Score;
}
参数和全局变量全写反了