索引器,能作为数据源,绑定到datagrid等吗?????请教谢谢!

fangxianghua9801 2005-12-30 10:13:55
using System;

namespace CsTest.IndexMethod
{
/// <summary>
/// 索引器的说明:
/// 索引器相似类的属性。只是索引器用来访问类中的数组对象元素,
/// 而属性用来访问类中的私有变量成员,所以索引器也可以用get,set访问函数,
/// 不同的是使用索引器取得的是对象中各元素的值,而不是特定的成员。
/// 定义属性时需要定义属性的名称,而索引器不需给出名称,只需使用关键字
/// this,它用于引用当前的对象实例,故this相当于当前类的对象名称。
/// </summary>
public class IndexStud
{
private string[] sname;
public IndexStud()//构造函数
{
sname=new string[]{"Tom","Seven","Steven","John"};
}
//定义类IndexStud的索引器,用于访问存储在snme数组中学生姓名信息
public string this[int index]
{
get
{
return sname[index];
}
set
{
sname[index]=value;
}
}
}
}
比如上面这个索引器,我在实际应用当中:
IndexStud Stud=new IndexStud();
Stud作为数据源绑定到DATAGRID好像不行,它不是集合类型的吗???不明白请教
...全文
107 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
建哥聊IT 2006-02-21
  • 打赏
  • 举报
回复
remark
真相重于对错 2005-12-30
  • 打赏
  • 举报
回复
webform datagrid.datasource 要求实现 IEnumerable
winform 要求实现IListSource 或 IList
alesso 2005-12-30
  • 打赏
  • 举报
回复
IList 包含 ICollection 和 IEnumerable 两个接口
如果只实现IEnumerable不行,就得实现完整的IList了
alesso 2005-12-30
  • 打赏
  • 举报
回复
或者你先试试只实现 IEnumerable 行不行
public class IndexStud : IEnumerable {
private string[] sname;
public IndexStud() {//构造函数
sname=new string[]{"Tom","Seven","Steven","John"};
}
//定义类IndexStud的索引器,用于访问存储在snme数组中学生姓名信息
public string this[int index] {
get {
return sname[index];
}
set {
sname[index]=value;
}
}
#region IEnumerable 成员

public IEnumerator GetEnumerator() {
return sname.GetEnumerator();
}

#endregion
}
fangxianghua9801 2005-12-30
  • 打赏
  • 举报
回复
help
fangxianghua9801 2005-12-30
  • 打赏
  • 举报
回复
alesso(卡卡)兄,谢谢,我还是不明白!
alesso 2005-12-30
  • 打赏
  • 举报
回复
你这样写


using System.Collections;
public class IndexStud : IList {
}

然后看看需要实现哪些方法
alesso 2005-12-30
  • 打赏
  • 举报
回复
实现IList接口就可以了

110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧