求助,C#达人进来看看吧

neell 2011-10-08 05:42:31
最近在写一个CLASS,咨询一个比较基础的问题。

CLASS中要写一个属性,该属性返回另一个自写的类。使用该CLASS实例时,属性后跟方括号,方括号中可以输入int或string类型;

例如,大家使用DataTable的实例A时,可以这样写A.Columns[0].Caption,也可以这样写A.Columns["xx"].Caption,并且在键盘输入"["的时候,会有summary的提示给到使用者。

问题就是这样,我也想写类似这样的类和属性,谁能帮助帮助我,并举例说明,谢谢;
...全文
271 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
assky124 2011-10-10
  • 打赏
  • 举报
回复
this.where = List<Person>.where

就是一个Lambda表达式
neell 2011-10-10
  • 打赏
  • 举报
回复
非常感谢楼上2位最终给出的代码,虽然2位的代码都不是我想要的(#16楼的代码,离真正目的还有一点距离;#17楼的代码this.Where只能靠猜,其实本质是和16楼的差不多)

还是非常感谢16,17楼2位给出了代码,非常感谢。
assky124 2011-10-10
  • 打赏
  • 举报
回复
就是索引器啊,一个Demo

public class Person
{
public string Name
{
get;
set;
}

public int Age
{
get;
set;
}

public double Height
{
get;
set;
}
}

public class PersonList : List<Person> //不用泛型可以用CollectionBase
{
public new Person this[int age]
{
get
{
return GetByAge(age);
}
set
{
Person p = GetByAge(age);
if (GetByAge(age) != null)
{
p = value;
}
else
{
throw new Exception("There is No Person Age IS Value");
}
}
}

private Person GetByAge(int age)
{
List<Person> list = this.Where(p => p.Age == age).ToList();
if (list.Count > 0)
{
return list[0];
}
else
{
return null;
}
}

//public Person this[string name]
//{
// get
// {
// }
// set
// {
// }
//}

//public Person this[double height]
//{
// get
// {
// }
// set
// {
// }
//}
}
ouyangin10 2011-10-10
  • 打赏
  • 举报
回复

class Test
{
public class Table
{
private Rows rows;
public Table()
{
rows = new Rows();
}
public Rows Rows
{
get { return rows; }
set { rows = value; }
}

}
public class Rows
{
public Hashtable hash = new Hashtable();
public Rows()
{
hash.Add(0, 100);
hash.Add("OK", 200);
}
public object this[int i]
{
get { return hash[i]; }
set { hash[i] = value; }
}
public object this[string s]
{
get { return hash[s]; }
set { hash[s] = value; }
}
}
static void Main()
{
Table t = new Table();
Console.WriteLine(t.Rows[0].ToString());
Console.WriteLine(t.Rows["OK"].ToString());

}
}

蛋疼代码.应该可以算上LZ说的引用方式吧?
k0mmDu 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 neell 的回复:]

首先,谢谢上述几位的贡献;
其次,不考虑泛型;
第三,索引器我知道,但是,请各位高手自己写写看这样的程序,属性返回的是另一个类(B),而不是int或string等基础类型,这种时候,索引器该如何应用。我来举例吧。

public class A
{
//此处假定属性名:property,返回类型为B;
}
public class B
{
public B(int i)……
[/Quote]
lz这样的这样的要求,索引器应该写在class B里面.另外索引器一般都是用于在集合中查找特定的实例的,不知道lz的需求如何
neell 2011-10-09
  • 打赏
  • 举报
回复
那就简单说吧,需要一个类似以下的效果:
DataTable a = new DataTable();
Console.WriteLine(a.Column[0].Caption);
Console.WriteLine(a.Column["xx"].Caption);

无所谓caption,只要能写2个类似DataTable与Column的类就可以了,能达到上面这样的引用方式就可以;
里面数据啊,内容啊什么都无所谓;只要能这样用就可以。

这次还有人没懂吗?
k0mmDu 2011-10-09
  • 打赏
  • 举报
回复
lz是要什么效果,根据索引器初始化class B?
我实在是看不懂耶.
diecode 2011-10-09
  • 打赏
  • 举报
回复
帮你顶一个
neell 2011-10-09
  • 打赏
  • 举报
回复
唉,我就少说一句,允许修改CLASS B;只要能做到我main中的意思就可以。

我也知道索引器是写在CLASS B中的,但是无论CLASS B的索引器怎么写,CLASS A都写不到我想达到的效果;之所以把CLASS B写成这样,是因为3个构造函数来表述更容易被人看懂;

OK,希望有人能上实例,不要再指导了,我要的是实际的代码。
neell 2011-10-08
  • 打赏
  • 举报
回复
自己顶一下,求解
yue_shanglin 2011-10-08
  • 打赏
  • 举报
回复
索引器啊索引器啊
neell 2011-10-08
  • 打赏
  • 举报
回复
首先,谢谢上述几位的贡献;
其次,不考虑泛型;
第三,索引器我知道,但是,请各位高手自己写写看这样的程序,属性返回的是另一个类(B),而不是int或string等基础类型,这种时候,索引器该如何应用。我来举例吧。

public class A
{
//此处假定属性名:property,返回类型为B;
}
public class B
{
public B(int i)
{...}
public B(double i)
{...}
public B(string i)
{...}
}

main()
{
A a = new A();
Console.WriteLine(a.property["aaa"].toString());
Console.WriteLine(a.property[0].toString());
Console.WriteLine(a.property[5.2].toString());
}

请各位高手帮忙补充class A的内容,特别是属性property,谢谢;
yangdabao 2011-10-08
  • 打赏
  • 举报
回复
it's complex
stonespace 2011-10-08
  • 打赏
  • 举报
回复
你自己参考一下DataTable.Columns不就行了,DataTable.Columns是一个新类并且定义了索引器,但其实最简单的方式不是索引器,而是直接把属性定义为List<XXX>类型,直接定义为数组,

比如:
class MyDataTable
{
private List<MyDataColumn> lstC=new List<MyDataColumn>();
public List<MyDataColumn> Columns
{
get
{
return lstC;
}
}
}

当然索引器也有索引器的好处,
xuwencan 2011-10-08
  • 打赏
  • 举报
回复
Dictionary<K,V>
k0mmDu 2011-10-08
  • 打赏
  • 举报
回复
k0mmDu 2011-10-08
  • 打赏
  • 举报
回复
索引器啊索引器

111,119

社区成员

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

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

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