c#构造函数中的枚举
c# enum in constructor
目前我正在做一个练习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
}
我没有看到声明您的 JobPosition
枚举的代码,但很可能您不需要以下行中的 "Enum."
部分:
administrationTeacher.AddTeacher(new Teacher(Enum.JobPosition.JUNIOR_Teacher, Convert.ToInt32(txtEc.Text), Convert.ToInt32(txtAge.Text), txtName.Text,
所以,你需要这样的东西
administrationTeacher.AddTeacher(new Teacher(JobPosition.JUNIOR_Teacher, Convert.ToInt32(txtEc.Text), Convert.ToInt32(txtAge.Text), txtName.Text,
P.S.: 别忘了在文件开头加上using.
using Here.Goes.Your.Enum.Namespace;
目前我正在做一个练习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
}
我没有看到声明您的 JobPosition
枚举的代码,但很可能您不需要以下行中的 "Enum."
部分:
administrationTeacher.AddTeacher(new Teacher(Enum.JobPosition.JUNIOR_Teacher, Convert.ToInt32(txtEc.Text), Convert.ToInt32(txtAge.Text), txtName.Text,
所以,你需要这样的东西
administrationTeacher.AddTeacher(new Teacher(JobPosition.JUNIOR_Teacher, Convert.ToInt32(txtEc.Text), Convert.ToInt32(txtAge.Text), txtName.Text,
P.S.: 别忘了在文件开头加上using.
using Here.Goes.Your.Enum.Namespace;