第一次海报长期用户。我正在为学校做ASP分配,我被卡住了!我花了10个小时试图自己解决它。
任务是建立学生注册系统。用户输入他们的姓名,选择他们的学生类型(兼职,全职或合作)并选择他们的课程(复选框)。然后,用户单击“提交”。然后,应用程序显示一个包含所选课程的ASP表。
兼职,全职和合作都是具有学生基类的所有课程。选择的课程存储在Student类中名为enrolledCourses的ArrayList中。 ArrayList受到保护。我有一个名为getEnrolledCourses的公共ArrayList方法,它返回enrolledCourses。该方法也在Student类中。
当用户单击“提交”时,该事件将执行一个名为buildTable(student)的方法。
在buildTable方法中,我创建了一个ASP表,并硬编码了表的第一行,即列名。然后我有一个foreach循环,假设输出每个课程。列名输出正常,但没有一个课程。
我的foreach声明是
foreach (Course course in student.getEnrolledCourses())
当我调试时,我收到错误:
getEnrolledCourses名称' getEnrolledCourses'不存在于 当前上下文getEnrolledCourses名称' getEnrolledCourses'不 在当前上下文中不存在
(是的,两次)
以下是我的网页代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CourseRegistration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
Header.Title = "Course Registration";
}
foreach (Course c in Helper.GetCourses())
{
CheckBox cb = new CheckBox();
cb.ID = "cb" + c.Code;
cb.Text = c.ToString();
Literal br = new Literal();
br.Text = "<br/>";
pnlCourses.Controls.Add(cb);
pnlCourses.Controls.Add(br);
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtName.Text != "" && (rdbtnCoop.Checked || rdbtnFull.Checked || rdbtnPart.Checked))
{
pnlFirst.Visible = false;
pnlSecond.Visible = true;
Student student = new FullTimeStudent(txtName.Text);
if (rdbtnFull.Checked)
student = new FullTimeStudent(txtName.Text);
else if (rdbtnPart.Checked)
student = new PartTimeStudent(txtName.Text);
else if (rdbtnCoop.Checked)
student = new CoopStudent(txtName.Text);
lblThanks.Text = "Thanks, <span class='error'>" + student.Name + "</span>, ";
lblInfo.Text = "Fill in";
buildTable(student);
}
}
protected void buildTable(Student student)
{
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thdCode = new TableHeaderCell();
TableHeaderCell thdTitle = new TableHeaderCell();
TableHeaderCell thdHours = new TableHeaderCell();
TableHeaderCell thdFee = new TableHeaderCell();
thdCode.Text = "Course Code";
thdTitle.Text = "Course Title";
thdHours.Text = "Weekly Hours";
thdFee.Text = "Fee Payable";
thr.Controls.Add(thdCode);
thr.Controls.Add(thdTitle);
thr.Controls.Add(thdHours);
thr.Controls.Add(thdFee);
tblCourses.Controls.Add(thr);
TableRow tr = new TableRow();
TableCell tdCode = new TableCell();
TableCell tdTitle = new TableCell();
TableCell tdHours = new TableCell();
TableCell tdFee = new TableCell();
int totalHours = 0;
double totalFee = 0.0;
foreach (Course course in student.getEnrolledCourses())
{
tdCode.Text = course.Code;
tdTitle.Text = course.Title;
totalHours += course.WeeklyHours;
tdHours.Text = course.WeeklyHours.ToString();
totalFee += course.Fee;
tdFee.Text = course.Fee.ToString();
tr.Controls.Add(tdCode);
tr.Controls.Add(tdTitle);
tr.Controls.Add(tdHours);
tr.Controls.Add(tdFee);
tblCourses.Controls.Add(tr);
}
//TableFooterRow tfr = new TableFooterRow();
}
}
这是学生班
using System;
using System.Collections;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Student
/// </summary>
public abstract class Student
{
private string name;
protected ArrayList enrolledCourses;
protected Student(string name)
{
this.name = name;
enrolledCourses = new ArrayList();
}
public string Name {
get { return name; }
}
public ArrayList getEnrolledCourses() {
return enrolledCourses;
}
public int totalWeeklyHours()
{
int totalHours = 0;
foreach (Course course in enrolledCourses) {
totalHours += course.WeeklyHours;
}
return totalHours;
}
public virtual double feePayable()
{
double totalFee = 0.0;
foreach (Course course in enrolledCourses) {
totalFee += course.Fee;
}
return totalFee;
}
public abstract void addCourse(Course course);
}
我非常感谢您提供的任何帮助。感谢。
答案 0 :(得分:-1)
您正在尝试枚举student.getEnrolledCourses()
到Course
对象的返回值,但是,抽象类返回值ArrayList
。
由于我们没有看到您Student
课程的具体实施,我们无法看到您的ArrayList
对象中的内容。
请通过更改:
来检查ArrayList
是否包含Course
个项目
foreach (Course course in student.getEnrolledCourses())
{
到
ArrayList list = student.getEnrolledCourses();
foreach (Course course in list)
{
然后我会在foreach
关键字上设置一个断点,并使用监视窗口检查list
的内容。
您可能还想检查Student
方法顶部的传入buildTable
对象是否为空:
if(student == null)
throw new ArgumentNullException("student");