如何在方法中调用类自身的索引器、方法?

w_l_851110 2010-12-08 11:36:34
如何在方法中调用自身类的索引器、方法?
public bool Contains(User user)
{
//user = new User();
if (user.IP == this[user.IP].IP)
{
return true;
}
else
{
return false;
}
}
public User this[string ip]
{
get
{
foreach (User u in List)
{
if (u.IP == ip)
{ return u; }
}
return null;
}
}

User 为另一个类,运行报错“未将对象引用设置到对象的实例。”将注释去掉仍然抱此错。
另外如果是调用自身类的方法呢?
求高人解答。。。
...全文
78 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
TimZhuFaith 2010-12-09
  • 打赏
  • 举报
回复
this[user.IP]这个你能保证它一定不为Null么
w_l_851110 2010-12-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 timzhufaith 的回复:]

1 List有没有实例化,有可能为null
2 public User this[string ip]可能会返回null,但
Contains方法直接用this[user.IP].IP,this[user.IP]是Contains方法的返回值
[/Quote]
返回为null,就不会相等啊,contains返回的是bool值
w_l_851110 2010-12-08
  • 打赏
  • 举报
回复
//user = new User();
但是我将注释去掉 为什么不行呢?郁闷哦。。。
w_l_851110 2010-12-08
  • 打赏
  • 举报
回复
吃个饭,脑袋清醒了点,应该是不能访问参数对象里的成员,所以会报错。。。
TimZhuFaith 2010-12-08
  • 打赏
  • 举报
回复
1 List有没有实例化,有可能为null
2 public User this[string ip]可能会返回null,但
Contains方法直接用this[user.IP].IP,this[user.IP]是Contains方法的返回值
w_l_851110 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]

namespace Study
{
class Program
{
static void Main(string[] args)
{
ScoreIndex s = new ScoreIndex();
s["张三", 1] = 90;
s["张三", 2] = 100;
s["张三", 3] = 80;
s["李四", 1] = 60;
……
[/Quote]
虽然你厉害,不过你没仔细看我的问题,貌似答非所问哦!
wuyq11 2010-12-08
  • 打赏
  • 举报
回复
namespace Study
{
class Program
{
static void Main(string[] args)
{
ScoreIndex s = new ScoreIndex();
s["张三", 1] = 90;
s["张三", 2] = 100;
s["张三", 3] = 80;
s["李四", 1] = 60;
s["李四", 2] = 70;
s["李四", 3] = 50;
Console.WriteLine("张三课程编号为1的成绩为:" + s["张三",1]);
Console.WriteLine("张三的所有成绩为:");
ArrayList temp;
temp = s["张三"];
foreach (IndexClass b in temp)
{
Console.WriteLine("姓名:" + b.Name + "课程编号:" + b.CourseID + "分数:" + b.Score);
}
Console.ReadKey();
}
}
class IndexClass
{
private string _name;
private int _courseid;
private int _score;
public IndexClass(string _name, int _courseid, int _score)
{
this._name = _name;
this._courseid = _courseid;
this._score = _score;
}
public string Name
{
get { return _name; }
set { this._name = value; }
}
public int CourseID
{
get { return _courseid; }
set { this._courseid = value; }
}
public int Score
{
get { return _score; }
set { this._score = value; }
}
}
class ScoreIndex
{
private ArrayList arr;
public ScoreIndex()
{
arr = new ArrayList();
}
public int this[string _name, int _courseid]
{
get
{
foreach (IndexClass a in arr)
{
if (a.Name == _name && a.CourseID == _courseid)
{
return a.Score;
}
}
return -1;
}
set
{
arr.Add(new IndexClass(_name, _courseid, value)); //arr["张三",1]=90
}
}
//重载索引器
public ArrayList this[string _name]
{
get
{
ArrayList temp = new ArrayList();
foreach (IndexClass b in arr)
{
if (b.Name == _name)
{
temp.Add(b);
}
}
return temp;
}
}
}
}

所有索引器都使用this关键词来取代方法名。Class或Struct只允许定义一个索引器,而且总是命名为this。
索引器允许类或结构的实例按照与数组相同的方式进行索引。索引器类似于属性,不同之处在于它们的访问器采用参数。
get 访问器返回值。set 访问器分配值。
this 关键字用于定义索引器。
value 关键字用于定义由 set 索引器分配的值

110,500

社区成员

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

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

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