如何实现属性,如同组数一样通过index读写? 本帖最后由 pfworld 于 2009-06-07 12:31:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class Topic { private string title01; public string Title01 { get { return title01; } set { title01 = value; } } private string title02; public string Title02 { get { return title02; } set { title02 = value; } } private string title03; public string Title03 { get { return title03; } set { title03 = value; } } public string this[int index] { get { switch (index) { case 0: return title01; case 1: return title02; case 2: return title03; default: return ""; } } set { switch (index) { case 0: title01 = value; return; case 1: title02 = value; return; case 2: title03 = value; return; default: return; } } } } 这样写 增加索引属性. 谢谢!!想起来了!!this索引器!!晕!! 对,用this撒,返回不同的类型,试试泛型 也可以返回object,在强制类型转换 public object this[int index] { get { switch (index) { case 0: return title01; case 1: return title02; case 2: return title03; default: return ""; } } set { switch (index) { case 0: title01 = value; return; case 1: title02 = value; return; case 2: title03 = value; return; default: return; } } 但是这个要进行数据转换!!有好的方法不? 使用 this 索引器。无法返回不同类型的值,有一个替代的方法是,返回 object 类型,引用类型没问题,值类型的话返回前boxing一下。然后在外部去unboxing 或者使用 as 操作符来还原类型。或者,你可以使用索引器重载public int this[int index]{}public string this[string index]{}.... 我现在要返回三种类型 string.double,bool 关键是索引器的参数不要确定,一般int,string,不知道还有什么号方法!!或者this.(int index,string index2,string index3)使用this[1,null,null]?? 帮忙看下这段代码检测打印机缺纸信息的 我想开发一个桌面软件自己用,希望各位好心人帮我解答几个问题 如何不关闭窗口刷新树视图treeview里面的内容。求高手前辈。 vs2008设置,怎么把圆点去掉? 问一个WINRAR 解压缩的问题 在线等!!一个关于AxWebBrowser小问题 Google Closure Templates 求助:字符串显示问题! 大家帮我看看 表更新的疑问 提供支持vs2010RTM版的最新Dotfuscator4.6.5000.746 DataTable和DefaultView的单元格分别是怎么定义的?
{
private string title01; public string Title01
{
get { return title01; }
set { title01 = value; }
} private string title02; public string Title02
{
get { return title02; }
set { title02 = value; }
} private string title03; public string Title03
{
get { return title03; }
set { title03 = value; }
} public string this[int index]
{
get
{
switch (index)
{
case 0:
return title01;
case 1:
return title02;
case 2:
return title03;
default:
return "";
}
}
set
{
switch (index)
{
case 0:
title01 = value;
return;
case 1:
title02 = value;
return;
case 2:
title03 = value;
return;
default:
return;
}
}
}
}
这样写 增加索引属性.
{
get
{
switch (index)
{
case 0:
return title01;
case 1:
return title02;
case 2:
return title03;
default:
return "";
}
}
set
{
switch (index)
{
case 0:
title01 = value;
return;
case 1:
title02 = value;
return;
case 2:
title03 = value;
return;
default:
return;
}
} 但是这个要进行数据转换!!有好的方法不?
{
}public string this[string index]
{
}....