-1

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
        }
    }
}
Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32

2 Answers2

1

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

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • Still no luck with the if statment. It's returning an error: Error 1 Operator '==' cannot be applied to operands of type 'string' and etc – Michael Caruana May 30 '15 at 11:43
  • It's getting all the types: books, newspaper and digital. I need it to display the type that the user inserted in the textbox for example if the user entered book only book will appear in the listbox, if the user entered digital only digital will appear in the listbox. – Michael Caruana May 30 '15 at 11:54
0

I include here 2 alternatives, depending on what you want to do. In both case the result filtering works:

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

I added this small constructor in the code just to be able to test the above.

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

If it doesn't work you'll have to explain further.

Guy
  • 1,232
  • 10
  • 21
  • .searchdep won't load when res. is written... is there any other way of how I can do it? – Michael Caruana May 30 '15 at 11:27
  • In my answer I have to guess, without really knowing what your code is. If you included the class Resource, it would make things easier. do you have a property with this name in your class? `public string searchdep { get; set; }` Can you explain a bit more what you are trying to do? – Guy May 30 '15 at 11:32
  • I added the resource class in my question and added the property you told me but still no luck – Michael Caruana May 30 '15 at 11:43
  • See my updated answer. I'm still not sure I completely understood: it's not clear what's the role of `string searchdep = txtDepartment.Text;` Is it going to replace `== DEPARTMENT.BOOK)` later on? Give me a few minutes. I think I understood. – Guy May 30 '15 at 11:56
  • searchdep is storing what is written in the textbox. No it's not going to replace ==department.book it's just reading text that the user written. – Michael Caruana May 30 '15 at 12:12
  • can you tell me where you added the last part pls? Thanks – Michael Caruana May 31 '15 at 06:36
  • If you refer to the alternative constructor I added, `public Resource(DEPARTMENT dep)` I added it at the top. You can add it anywhere, though constructors are usually at the top, like properties. In your real application you would probably want to keep you existing constructor, and simply add `DEPARTMENT dep` inside. This is your existing constructor. `public Resource(string refno, string title, string status)`. I would make it `public Resource(string refno, string title, string status, DEPARTMENT dep)` Tell me, does it work? – Guy May 31 '15 at 06:54