【发布时间】:2021-09-13 18:56:06
【问题描述】:
目前我正在开发一个用于练习 C# 的学生系统注册。我试图在构造函数中添加一个枚举,但得到错误:枚举不包含 JobPosition 的定义。我完全不知道可能出了什么问题,并尝试了不同的方法。有没有人知道问题出在哪里?
添加老师的代码:
//Begin adding a Teacher
private void btnNewTeacher_Click(object sender, EventArgs e)
{
if (txtEc.Text == "" || txtAge.Text == "" || txtName.Text == "" || txtPcn.Text == "" || txtYearAtSchool.Text == "")
{
MessageBox.Show("Please fill in all the required information");
}
else
{
//Check if pcn is used, if not add student
if (administrationTeacher.GetTeacher(Convert.ToInt32(txtPcn.Text)) == null
&& administrationStudent.GetStudent(Convert.ToInt32(txtPcn.Text)) == null)
{
administrationTeacher.AddTeacher(new Teacher(Enum.JobPosition.JUNIOR_Teacher, Convert.ToInt32(txtEc.Text), Convert.ToInt32(txtAge.Text), txtName.Text,
Convert.ToInt32(txtPcn.Text), Convert.ToInt32(txtYearAtSchool.Text)));
}
else
{
MessageBox.Show("The pcn for this student is already in use, " +
"please pick another one");
}
}
}
教师代码:
public JobPosition jobPosition { get; private set; }
private double salary;
public Teacher(JobPosition jobPosition, double salary, int age, string name, int pcn, int yearAtSchool)
: base(age, name, pcn, yearAtSchool)
{
this.jobPosition = jobPosition;
this.salary = salary;
}
JobPosition接口代码:
public enum JobPosition //Enum represents a group of constants.
{
JUNIOR_TEACHER,
TEACHER1,
TEACHER2,
TEACHER3,
COORDINATOR,
DIRECTOR
}
【问题讨论】:
-
为什么选择 Enum.JobPosition.JUNIOR_Teacher?只是 JobPosition.JUNIOR_Teacher,确定吗? (注意:JUNIOR_Teacher 作为一个名字也可能不寻常)
-
我认为您不了解枚举,这可能会有所帮助。 Enumeration types (C# reference)
标签: c# enums constructor interface