1

我创建了一个外部类名“Enum”,然后在其中创建了一个名为“UnderGraduate”的内部类

然后在那堂课上我做了一个这样的字符串

 class Enum
    {
     public class UnderGraduate
        {
            public string[] EducationUG=new string[]
        {
            ("Bachelor of Arts (B.A)"),
            ("Bachelor of Arts (Bachelor of Education (B.A. B.Ed)"),
            ("Bachelor of Arts (Bachelor of Law (B.A.B.L)"),
            ("Bachelor of Arts (Bachelor of Law (B.A.LLB)"),
            ("Bachelor of Ayurvedic Medicine and Surgery (B.A.M.S)"),
            ("Bachelor of Applied Sciences (B.A.S)")
}
}
}

现在我想在主类中调用这个字符串,并将其元素添加到一个下拉列表中,使用 for 循环在上一个下拉列表的 SelectedIndexChange 上使用编码

private void edulvlcb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (edulvlcb.SelectedItem.ToString() == "UnderGraduate")
            {         
                educb.Items.Clear();
                foreach (string ug in new Enum.UnderGraduate.EducationUG[])
                {
                    educb.Items.Add(new ListItem(EducationUG[name].ToString()));

                }

但它显示一个错误

An Object Reference is required for non-static field ,method or property 'Project.Enum.UnderGraduate.EducationUG'

请为我解决这个问题.......

4

1 回答 1

0

像这样更改您的代码:(不要使用 Enum 之类的保留字!)

class Enum1
{
    public class UnderGraduate
    {
        public static string[] EducationUG=new string[]{
            "Bachelor of Arts (B.A)",
            "Bachelor of Arts (Bachelor of Education (B.A. B.Ed)",
            "Bachelor of Arts (Bachelor of Law (B.A.B.L)",
            "Bachelor of Arts (Bachelor of Law (B.A.LLB)",
            "Bachelor of Ayurvedic Medicine and Surgery (B.A.M.S)",
            "Bachelor of Applied Sciences (B.A.S)"}
    }
}


private void edulvlcb_SelectedIndexChanged(object sender, EventArgs e)
{
    if (edulvlcb.SelectedItem.ToString() == "UnderGraduate")
    {         
        educb.Items.Clear();
        foreach (string ug in Enum1.UnderGraduate.EducationUG)
            educb.Items.Add(ug);
    }
}
于 2013-04-27T15:22:20.617 回答