请教c#简单的索引器问题

chang_heng 2009-04-29 10:52:02
我是c#新手写了个程序一直报错 请大家帮我看看
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace 多个参数的索引器
{
//姓名,课程ID,成绩
class CourseScore //课程的分数类
{
private string name;
private int courseID;
private int score;
public CourseScore(string name, int courseID, int score)
{
this.name = name;
this.courseID = courseID;
this.score = score;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int CourseID
{
get { return courseID; }
set { courseID = value; }
}
public int Score
{
get { return score; }
set { score = value; }
}
}
class CourseScoreIndexer //索引器,用于存取和查询CourseScore
{
private ArrayList arrCourseScore;
public CourseScoreIndexer()
{
arrCourseScore=new ArrayList();
}
public int this[string name, int courseID]
{
get

{
foreach(CourseScore cs in arrCourseScore)
{
if(cs.Name==name && cs.CourseID==courseID)
{
return cs.Score;
}
return -1;
}
}
set
{
arrCourseScore.Add(new CourseScore(name,courseID,value));
}
}
}


class Program
{
static void Main(string[] args)
{
CourseScoreIndexer csi =new CourseScoreIndexer();
csi["张三",1]=90;
csi["张三",2]=80;
csi["张三",3]=85;
csi["李四",1]=80;
Console.WriteLine(csi["张三",2]);



}
}
}
错误信息 错误 1 “多个参数的索引器.CourseScoreIndexer.this[string, int].get”: 并非所有的代码路径都返回值 D:\Backup\我的文档\Visual Studio 2005\Projects\第二章\多个参数的索引器\多个参数的索引器\Program.cs 45 13 多个参数的索引器
...全文
106 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljhcy99 2009-04-29
  • 打赏
  • 举报
回复
namespace 多个参数的索引器
{
//姓名,课程ID,成绩
class CourseScore //课程的分数类
{
private string name;
private int courseID;
private int score;
public CourseScore(string name, int courseID, int score)
{
this.name = name;
this.courseID = courseID;
this.score = score;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int CourseID
{
get { return courseID; }
set { courseID = value; }
}
public int Score
{
get { return score; }
set { score = value; }
}
}
class CourseScoreIndexer //索引器,用于存取和查询CourseScore
{
private ArrayList arrCourseScore;
public CourseScoreIndexer()
{
arrCourseScore=new ArrayList();
}
public int this[string name, int courseID]
{
get

{
foreach(CourseScore cs in arrCourseScore)
{
if(cs.Name==name && cs.CourseID==courseID)
{
return cs.Score;
}
else continue;
}
return -1;
}
set
{
arrCourseScore.Add(new CourseScore(name,courseID,value));
}
}
}


class Program
{
static void Main(string[] args)
{
CourseScoreIndexer csi =new CourseScoreIndexer();
csi["张三",1]=90;
csi["张三",2]=80;
csi["张三",3]=85;
csi["李四",1]=80;
Console.WriteLine(csi["张三",2]);



}
}
}
gomoku 2009-04-29
  • 打赏
  • 举报
回复
 
get
{
foreach(CourseScore cs in arrCourseScore)
{
if(cs.Name==name && cs.CourseID==courseID)
{
return cs.Score;
}
return -1;
}
}

foreach当arrCourseScore的“长度”如果为零的时候,括号内的语句将不会执行,对不对?
也就是说return cs.Score; 或return -1;语句可能不会执行,这种情况下getter就没有返回值了。

因而编译器抱怨“并非所有的代码路径都返回值”。



最近一直在研究爬虫和Lucene,虽然开始决定选用Heritrix来执行爬虫操作,但是后来发现用它来做还是存在一定的问题,比如需要程序生成相应的XML文件,对于同一个Job,怎样才能保证重复运行该Job时文件夹始终是同一个(Heritrix为Job创建文件夹的规则是“Job名称-时间戳”)等等,都是需要考虑的问题,最终还是将其搁浅。    后来google了一下,找到了一个简单爬虫的程序代码(http://www.blogjava.net/Jack2007/archive/2008/03/24/188138.html),随即试验了一下,发现确实能得到网页的内容,在这里还是要谢谢代码的提供者——Jack.Wang。    虽然试验成功,但是在随后的大数据量试验时,还是出现了问题。最初试验时,我只是让程序去抓取10个URL链接,当我将URL链接数改为100个时,问题出现了——URL中存在重复,而且非常容易的就变成死循环。举个例子来说,比如我首先爬的是A.html,在A.html中有两个链接:B.html,C.html,等爬完A.html以后,程序会爬B.html,这时如果B.html中的所有链接中有A.html这个页面的链接,那么程序又会去爬A.html这个页面,如此一来就形成了一个死循环,永远也不能停止。    跟踪程序发现,原来是在添加要抓取的网页的链接列表中,没有将已经抓取过的URL去除,所以才造成了死循环。现在虽然加上了这个判断,但是从我运行程序的效果来看,也不是很理想,总是感觉有些慢,800个页面要一两分钟才能爬完,这个我觉得有点说不过去。    这个产品,做到现在,我遇到了这么几个情况,有和大家分享的,也有向大家请教,求助的。    1.关于对应关系数据的保存方式    在创建索引的时候,需要将网页的URL和网页的内容传到相应的方法中,当然URL和内容是要对应的,也许是经验太少吧,我采取的是通过构建一个JavaBean的方式来传递的,不知道大家有没有更好的方法       2.关于要创建索引的内容的保存方式    最初的想法是不创建文件,直接将内容保存到变量中,然后创建索引,即先抓取网页的内容,然后将网页的内容和URL保存到自己构建的JavaBean对象中,接着将这个对象放到一个list列表中,等所有网页抓取完毕以后,将这个列表传到创建索引的方法中。这种做法看似不错,可是当URL数量很大时,会导致内存不够用,所以还是创建文件比较稳妥。    3.关于网页编码问题    遇到这个问题也是一个巧合,本来我抓取的是客户的一个网站,后来同事说如果客户看访问日志,这个月的数据会和平常的数据不一样,所以我就抓取公司的网站,结果,问题出现了。原先公司的网站是用GB2312编码做的页面,现在采用的是UTF-8的编码,虽然我已经判断了页面的编码,可是依然不能解决保存的文件中文乱码的问题,不知道大家有什么好办法没有。错误信息为:java.io.UnsupportedEncodingException    附件为爬虫代码 本文出自 “徘徊在c#,java,php之间” 博客,请务必保留此出处http://jerrysun.blog.51cto.com/745955/221879

111,126

社区成员

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

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

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