【问题标题】:Enumerations Foreach Loop C#枚举 Foreach 循环 C#
【发布时间】:2015-05-30 11:17:35
【问题描述】:

我正在尝试根据用户在 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
        }
    }
}

【问题讨论】:

    标签: c# foreach enums


    【解决方案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 块,因为没有必要在 foreach 循环内重复检查 searchdep 的值。)

    【讨论】:

    • if 语句仍然没有运气。它返回一个错误:错误 1 ​​运算符“==”不能应用于“字符串”等类型的操作数
    • 它涵盖了所有类型:书籍、报纸和数字。我需要它来显示用户在文本框中插入的类型,例如,如果用户输入的只有书本将出现在列表框中,如果用户输入数字,则只有数字会出现在列表框中。
    【解决方案2】:

    我在这里包括 2 个备选方案,具体取决于您想要做什么。在这两种情况下,结果过滤都有效:

        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();
        }
    

    我在代码中添加了这个小构造函数只是为了能够测试上述内容。

    public class Resource : IComparable
        {
            public Resource(DEPARTMENT dep)
            {
                department = dep;
            }
             ...
        }
    

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

    【讨论】:

    • .searchdep 在 res 时不会加载。写...有没有其他方法可以做到这一点?
    • 在我的回答中,我不得不猜测,而不知道你的代码是什么。如果您包含类资源,它会使事情变得更容易。你的班级有同名的房产吗? public string searchdep { get; set; }你能解释一下你想要做什么吗?
    • 我在我的问题中添加了资源类并添加了您告诉我的属性但仍然没有运气
    • 查看我的更新答案。我仍然不确定我是否完全理解:不清楚string searchdep = txtDepartment.Text; 的作用是什么,以后会取代== DEPARTMENT.BOOK) 吗?给我几分钟。我想我明白了。
    • searchdep 存储文本框中的内容。不,它不会取代 ==department.book 它只是阅读用户编写的文本。
    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多