【发布时间】: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
}
}
}
【问题讨论】: