【问题标题】:c# enum in constructorc# 在构造函数中枚举
【发布时间】: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


【解决方案1】:

我还没有看到声明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;

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    • 2011-07-03
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多