怎样给类成员动态赋值?100分送上!
/// <summary>
/// 实现IComparable接口,用kcNum做比较
/// </summary>
/// <param name="obj">比较对象</param>
/// <returns>比较结果</returns>
public class KeChen : IComparable
{
public string kcName { get; set; }
public int kcNum { get; set; }
public int CompareTo(object obj)
{
if (obj is KeChen)
{
return kcNum.CompareTo(((KeChen)obj).kcNum);
}
return 1;
}
}
……
string strkc = "";
//初始化课程类并排序 成绩提高最明显的课程
KeChen[] KC = new KeChen[] {
new KeChen(){kcName="基础护理",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][7])},
new KeChen(){kcName="外科护理",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][8])},
new KeChen(){kcName="内科护理",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][9])},
new KeChen(){kcName="妇科护理",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][10])},
new KeChen(){kcName="儿科护理",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][11])},
new KeChen(){kcName="其它",kcNum=Convert.ToInt32( ds2.Tables[0].Rows[0][12])}};
Array.Sort(KC);
Array.ForEach<KeChen>(KC, (m) =>
{
strkc = strkc + m.kcName +" "+ m.kcNum.ToString()+" ";
});
现在想把从数据库搜索出来的记录给类的成员动态赋值,循环语句应该怎么写?我试着写了下,报错。
for (int i = 0; i < ds.Tables["BB"].Rows.Count; i++)
{
// new KeChen() { kcName = ds.Tables["BB"].Rows[i][1].ToString(), kcNum = strTnum1[i] };
T1[i].kcName = ds.Tables["BB"].Rows[i][1].ToString();
T1[i].kcNum = strTnum1[i];
}
请教,应该怎么样写?