Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...