枚举Foreach循环C#

时间:2015-05-30 11:17:35

标签: c# foreach enums

我正在尝试根据用户在txtDepartment.Text中输入的文本列出多个字符串。但它显示的所有项目我不仅仅是BOOK类型。 DEPARTMENT是枚举,它具有BOOK,NEWSPAPER和DIGITAL类型的值。任何人都知道如何只显示BOOK类型而不是资源列表中的所有项目?下面是我到目前为止的代码。

string searchdep = txtDepartment.Text;
        foreach(Resource res in resourceslist)
        {
            if(searchdep==DEPARTMENT.BOOK)
            {
                lbResult.Items.Add(res);
            }
        }

这是我的课程资源

namespace OOP_V1._3
{
    public enum DEPARTMENT
    {
       BOOK,
       NEWSPAPER,
       DIGITAL
    }

    public enum STATUS
    {
        AVALIABLE,
        BORROWED,
        RESERVED
    }

    [Serializable]
    public abstract class Resource : IComparable
    {
        public string title;
        public string refno;
        public DEPARTMENT department;
        public STATUS status;

        public string searchdep { get; set; }

        public string getTitle()
        {
            return title;
        }

        public void setTitle(string iTitle)
        {
            this.title = iTitle;
        }

        public string getRefno()
        {
            return refno;
        }

        public void setRefno(string iRefno)
        {
            this.refno = iRefno;
        }

        public DEPARTMENT getDepartment()
        {
            return department;
        }

        public void setDepartment(DEPARTMENT iDepartment)
        {
            this.department = iDepartment;
        }

        public STATUS  getStatus()
        {
            return status;
        }

        public void setStatus(STATUS iStatus)
        {
            this.status = iStatus;
        }

        public override string ToString() //display the books in the form of a string
        {
            return String.Format("Ref No: {0}, Title: {1}, Department: {2}, Status: {3}", refno, title, department, status);
        }

        public Resource(string refno, string title, string status)
        {
            this.refno = refno;
            this.title = title;

            if (status == "Available")
            {
                this.status = STATUS.AVALIABLE;
            }
            else if (status == "Borrowed")
            {
                this.status = STATUS.BORROWED;
            }
            else if (status == "Reserved")
            {
                this.status = STATUS.RESERVED;
            }
        }

        public int CompareTo(Object obj)
        {
            Resource other = (Resource)obj;

            int c = string.Compare(this.getTitle(), other.getTitle()); //comparing the title inputted by the user with a title from the list

            return c; //returning the answer
        }
    }
}

2 个答案:

答案 0 :(得分:1)

首先,我建议将用户的值解析为Enum,然后在输入无效时采取适当的措施。您可以使用Enum.TryParse方法执行此操作:

DEPARTMENT result;
if (!Enum.TryParse(searchdep, true, out result))
{
    // display error message?
    return;
}

然后,您可以使用该解析值进行比较:

if (result == DEPARTMENT.BOOK)
{
    foreach (Resource res in resourceslist)
    {
        lbResult.Items.Add(res);
    }
}

(我已经翻转了ifforeach块,因为无需在searchdep循环内反复检查foreach的值。)

答案 1 :(得分:0)

我在这里包含2个替代方案,具体取决于您的目的。在这两种情况下,结果过滤都有效:

jmpq

我在代码中添加了这个小构造函数,以便能够测试上面的内容。

    static void Main(string[] args)
    {
        List<Resource> Resources = new List<Resource> { new Resource(DEPARTMENT.DIGITAL), new Resource(DEPARTMENT.BOOK) };
        List<Resource> lbResult = new List<Resource>();
        string searchdep = "BOOK";
        DEPARTMENT result;
        //if (Enum.TryParse(searchdep, true, out result))
            foreach (var res in Resources)
                if (res.department == DEPARTMENT.BOOK) //or instead == result.
                    lbResult.Add(res);
        Console.ReadLine();
    }

如果它不起作用,你将不得不进一步解释。