Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 每个循环C的枚举数#_C#_Foreach_Enums - Fatal编程技术网

C# 每个循环C的枚举数#

C# 每个循环C的枚举数#,c#,foreach,enums,C#,Foreach,Enums,我试图根据用户在txtDepartment.text中输入的文本列出一些字符串。但它显示了我所有的项目,不仅仅是图书类型。部门是枚举的,它具有图书、报纸和数字类型的值。有人知道我如何只显示BOOK类型而不是ResourcesList中的所有项目吗?下面是我到目前为止的代码 string searchdep = txtDepartment.Text; foreach(Resource res in resourceslist) { if(se

我试图根据用户在txtDepartment.text中输入的文本列出一些字符串。但它显示了我所有的项目,不仅仅是图书类型。部门是枚举的,它具有图书、报纸和数字类型的值。有人知道我如何只显示BOOK类型而不是ResourcesList中的所有项目吗?下面是我到目前为止的代码

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
        }
    }
}

我在这里包括两种选择,这取决于你想做什么。在这两种情况下,结果过滤都起作用:

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

如果不起作用,您必须进一步解释。

首先,我建议将用户的值解析到您的枚举中,然后在输入无效时采取一些适当的操作。可以使用
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);
    }
}

(我已经翻转了你的
if
foreach
块,因为没有必要在
foreach
循环中反复检查
searchdep
的值。)

.searchdep在写入res.时不会加载。。。有没有其他方法可以做到这一点?在我的回答中,我不得不猜测,而不知道你的代码是什么。如果包含类资源,将使事情变得更简单。您的类中是否有具有此名称的属性
publicstringsearchdep{get;set;}
你能再解释一下你想做什么吗?我在我的问题中添加了资源类,并添加了你告诉我的属性,但仍然没有看到我的更新答案。我仍然不确定自己是否完全理解了:还不清楚
stringsearchdep=txtDepartment.Text的作用是什么
以后它会取代
==DEPARTMENT.BOOK)
吗?给我几分钟。我想我明白了。searchdep正在存储文本框中写入的内容。不,它不会替换==department.book,它只是在阅读用户编写的文本。if语句仍然没有成功。它返回了一个错误:错误1运算符“==”无法应用于“string”类型的操作数,并且etcIt正在获取所有类型:书籍、报纸和数字。我需要它显示用户在文本框中插入的类型,例如,如果用户输入的仅限书本将显示在列表框中,如果用户输入的仅限数字将显示在列表框中。
if (result == DEPARTMENT.BOOK)
{
    foreach (Resource res in resourceslist)
    {
        lbResult.Items.Add(res);
    }
}