枚举Foreach循环C# [英] Enumerations Foreach Loop C#
问题描述
我试图根据用户在txtDepartment.Text中输入的文本列出一些字符串.但是它显示的是我不仅具有BOOK类型的所有项目. DEPARTMENT是枚举,其值的类型为BOOK,NEWSPAPER和DIGITAL.有谁知道我只能显示BOOK类型,而不能显示资源列表中的所有项目?下面是我到目前为止的代码.
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);
}
}
这是我的课程资源
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
}
}
}
推荐答案
首先,我建议将用户的值解析为您的Enum,然后在输入无效的情况下采取适当的措施.您可以使用Enum.TryParse
方法做到这一点:
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);
}
}
(我已经翻转了您的if
和foreach
块,因为不需要在该foreach
循环内重复检查searchdep
的值.)
(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.)
这篇关于枚举Foreach循环C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!