Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

Enumerations Foreach Loop C#

I'm trying to list a number of strings according to the text that the user entered in txtDepartment.Text. But it is displaying all the items that I have not only of type BOOK. DEPARTMENT is enumeration and it has values of type BOOK, NEWSPAPER and DIGITAL. Anyone know how I can display only of type BOOK not all the items in resourceslists? Below is the code I have so far.

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

This is my class Resouce

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
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, I'd recommend parsing the user's value into your Enum, and then take some appropriate action if the input is invalid. You can do that using the Enum.TryParse method:

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

Then, you can use that parsed value for comparison:

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

(I've flipped your if and foreach blocks around, because there's no need to check the value of searchdep repeatedly inside that foreach loop.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...