ASP.NETクラス用のプログラムを作成して、クラス名と番号を作成し、学生IDを使用して10人の学生名を入力しました。したがって、私のWebページでは、 MyClass(コース名と番号)とStudentクラス(学生の名前とIDを保持するクラス)に分けるこれらの4つの情報を入力するようにユーザーに求めます。私はNullReferenceExceptionエラーでひどく立ち往生していて、何をすべきかわかりません。ここにいる誰かが私にいくつかの指針と方向性を教えてくれれば、私はこの課題から自分自身を拾うことができます。1つ確かなことは、新しいメソッドや変数を入力することは許可されていないということです。初期化した現在のものしか使用できません。
- Student []配列は、学生の名前とIDを持つStudentクラスを保持する配列です。
- ユーザーは最初にクラス番号と名前を作成し、後で生徒を追加します(IDと名前を使用)。
- クラス名と番号(一緒に)は1つだけ許可され、最大10人の学生のみが許可されます。
MyClassのToString()メソッドでnull例外が発生します。ここで、classNumberAndName + =((Student)students [i])。ToString();
これが構文エラーのない私のコードです。
public class MyClass
{
private string courseNumber;
private string courseName;
private int numberOfStudents;
private Student[] students;
public MyClass(string CourseNumber , string CourseName, Student[] Student)
{
students = new Student[Student.Length];
courseNumber = CourseNumber;
courseName = CourseName;
for (int i = 0; i < Student.Length; i++)
{
Student tmpArrayStudent = new Student(((Student)students[i]).ToString(),((Student)students[i]).ToString());
tmpArrayStudent = students[i];
}
}
public MyClass(string CourseNumber, string CourseName)
{
courseNumber = CourseNumber;
courseName = CourseName;
students = new Student[10];
}
public void addAStudent(Student student)
{//possible to create temp array?
Student[] students = new Student[10];
if(numberOfStudents < students.Length)
{
students[numberOfStudents] = student;
numberOfStudents++;
}
else
{
throw new Exception("Amount of Students Exceeded, no more than 10");
}
}
public string getClassNumberAndName()
{
return "Course Number: " + courseNumber + " " + " Course Name: " + courseName;
}
public override string ToString()
{
string classNumberAndName = getClassNumberAndName();
for(int i = 0; i < students.Length; i++)
{
classNumberAndName += ((Student)students[i]).ToString();
}
classNumberAndName += "Students Registered: " + numberOfStudents.ToString();
return classNumberAndName;
}
}
public class Student
{
private string idNumber;
private string name;
public Student(string ID, string Name)
{
idNumber = ID;
name = Name;
}
public override string ToString()
{
return "Student ID: " + idNumber + " " + " Student Name " + name;
}
}
そして、これがWebコードの.aspx.csファイルです。
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
pnlStudent.Visible = false;//use panels to disable the ability to enter new course information
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
MyClass course = new MyClass(txtCnum.Text, txtCname.Text);
Session.Add("Course", course);
((MyClass)Session["Course"]).addAStudent(new Student(txtCnum.Text, txtCname.Text));//since we entered to Student, course information or student information?
txtAnswers.Text += course.ToString();
pnlStudent.Visible = true;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Student student = new Student(txtID.Text, txtName.Text);
pnlCourse.Visible = false;
try
{
((MyClass)Session["Course"]).addAStudent(new Student(txtID.Text, txtName.Text));
txtAnswers.Text += Session["Course"].ToString();
}
catch(Exception ex)
{
txtAnswers.Text += "Amount of students are at maximum." + ex.Message;
}
}
}