C#上下文外方法
第一次海报长期用户。我正在为学校做一个ASP作业,我被卡住了!我花了10个小时试图自己解决这个问题 任务是建立一个学生注册系统。用户输入他们的姓名,选择他们的学生类型(兼职、全职或合作),并选择他们的课程(复选框)。然后用户单击submit。然后,应用程序将显示一个包含所选课程的ASP表 兼职、全日制和合作制都是以学生为基础的班级。所选课程存储在学生类中名为Enrolled courses的ArrayList中。ArrayList受保护。我有一个名为getEnrolledCourses的公共ArrayList方法,它返回enrolledCourses。该方法也在学生课堂中使用 当用户单击submit时,事件将执行一个名为buildTable(student)的方法 在buildTable方法中,我创建了一个ASP表,并对表的第一行(列名)进行了硬编码。然后我有一个foreach循环,它假设输出每个课程。列名输出很好,但没有一个课程输出 我的foreach声明是C#上下文外方法,c#,asp.net,C#,Asp.net,第一次海报长期用户。我正在为学校做一个ASP作业,我被卡住了!我花了10个小时试图自己解决这个问题 任务是建立一个学生注册系统。用户输入他们的姓名,选择他们的学生类型(兼职、全职或合作),并选择他们的课程(复选框)。然后用户单击submit。然后,应用程序将显示一个包含所选课程的ASP表 兼职、全日制和合作制都是以学生为基础的班级。所选课程存储在学生类中名为Enrolled courses的ArrayList中。ArrayList受保护。我有一个名为getEnrolledCourses的公共Ar
foreach (Course course in student.getEnrolledCourses())
当我调试时,会出现以下错误:
GetEnrolled课程名称“GetEnrolled课程”在中不存在
当前上下文GetEnrolled课程名称“GetEnrolled课程”不包含
在当前上下文中不存在
(是的,两次)
以下是我的网页背后的代码:
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();
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
公共部分课程注册:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack){
Header.Title=“课程注册”;
}
foreach(Helper.GetCourses()中的课程c)
{
复选框cb=新复选框();
cb.ID=“cb”+c.代码;
cb.Text=c.ToString();
文字br=新文字();
br.Text=“
”;
pnlCourses.Controls.Add(cb);
pnlCourses.Controls.Add(br);
}
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
if(txtName.Text!=“”&(rdbtnCoop.Checked | | | rdbtnFull.Checked | | | rdbtnPart.Checked))
{
pnlFirst.Visible=false;
pnlSecond.Visible=真;
学生=新的全日制学生(txtName.Text);
如果(rdbtnFull.Checked)
学生=新的全日制学生(txtName.Text);
否则如果(rdbtnPart.Checked)
学生=新的兼职学生(txtName.Text);
else if(选中rdbtnCoop)
学生=新的合作学生(txtName.Text);
lblThanks.Text=“谢谢,”+student.Name+“,”;
lblInfo.Text=“填写”;
构建表(学生);
}
}
受保护的void构建表(学生)
{
TableHeaderRow thr=新的TableHeaderRow();
TableHeaderCell thdCode=新的TableHeaderCell();
TableHeaderCell thdTitle=新的TableHeaderCell();
TableHeaderCell thdHours=新的TableHeaderCell();
TableHeaderCell thdFee=新的TableHeaderCell();
thdCode.Text=“课程代码”;
thdTitle.Text=“课程名称”;
thdHours.Text=“每周工作时间”;
thdFee.Text=“应付费用”;
thr.Controls.Add(thdCode);
thr.Controls.Add(thdTitle);
thr.Controls.Add(thh);
thr.Controls.Add(thdFee);
TBL课程。控制。添加(thr);
TableRow tr=新的TableRow();
TableCell tdCode=新的TableCell();
TableCell tdTitle=新的TableCell();
TableCell tdHours=新的TableCell();
TableCell tdFee=新的TableCell();
整数总小时=0;
双倍总费用=0.0;
foreach(student.getEnrolledCourses()中的课程)
{
tdCode.Text=课程代码;
tdTitle.Text=课程名称;
总时数+=课程周时数;
tdHours.Text=course.WeeklyHours.ToString();
总费用+=课程费用;
tdFee.Text=course.Fee.ToString();
tr.Controls.Add(tdCode);
tr.Controls.Add(tdTitle);
tr.Controls.Add(TDH);
tr.Controls.Add(tdFee);
tblCourses.Controls.Add(tr);
}
//TableFooterRow tfr=新的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);
}
使用系统;
使用系统集合;
使用System.Linq;
使用System.Web;
///
///学生简介
///
公共抽象班学生
{
私有字符串名称;
受保护的ArrayList注册课程;
受保护学生(字符串名称)
{
this.name=名称;
Enrolled Courses=新建ArrayList();
}
公共字符串名{
获取{返回名称;}
}
公共阵列列表GetEnrolled课程(){
返回已注册课程;
}
公共int totalWeeklyHours()
{
整数总小时=0;
foreach(注册课程中的课程){
总时数+=课程周时数;
}
返回总小时数;
}
公共虚拟双代号()
{
双倍总费用=0.0;
foreach(注册课程中的课程){
总费用+=课程费用;
}
返回总费用;
}
公共抽象课程(课程);
}
如果您能提供任何帮助,我将不胜感激。谢谢。您正试图将
student.getEnrolled Courses()
的返回值枚举到课程
对象,但是,抽象类返回的值为数组列表
由于我们没有看到Student
类的具体实现,我们无法看到ArrayList
对象中的项目
请检查foreach (Course course in student.getEnrolledCourses())
{
ArrayList list = student.getEnrolledCourses();
foreach (Course course in list)
{
if(student == null)
throw new ArgumentNullException("student");