当前位置: 代码迷 >> ASP.NET >> 请教这样调用的函数如何写
  详细解决方案

请教这样调用的函数如何写

热度:258   发布时间:2013-02-26 00:00:00.0
请问这样调用的函数怎么写
引用functionname[ "属性名 "]
然后就可以得到值了

------解决方案--------------------------------------------------------
get.........
set............
------解决方案--------------------------------------------------------
是问集合的写法?
------解决方案--------------------------------------------------------
functionname[ "属性名 "]

索引器的使用,
------解决方案--------------------------------------------------------
get
{

}
set
{

}
------解决方案--------------------------------------------------------
当然可以了……给你一个例子。

class DayCollection
{
string[] days = { "Sun ", "Mon ", "Tues ", "Wed ", "Thurs ", "Fri ", "Sat " };

// This method finds the day or returns -1
private int GetDay(string testDay)
{
int i = 0;
foreach (string day in days)
{
if (day == testDay)
{
return i;
}
i++;
}
return -1;
}

// The get accessor returns an integer for a given string
public int this[string day]
{
get
{
return (GetDay(day));
}
}
}

class Program
{
static void Main(string[] args)
{
DayCollection week = new DayCollection();
System.Console.WriteLine(week[ "Fri "]);
System.Console.WriteLine(week[ "Made-up Day "]);
}
}
  相关解决方案