我的程序有一个人员列表和事件列表,这个程序的工作就是将这些事件的门票卖给列表中的人。
我是一个我需要在该功能中编程的舞台,该功能允许用户选择他们想要购买门票的节目/活动。我决定通过在RichTextBox中显示可用事件并要求用户写下他们想要购买门票的事件的名称来做到这一点。如果此事件存在,则程序应进入客户详细信息表单;如果没有那么它应该重置输入TextBox并向用户显示错误消息 - 这很简单。
但是,出于某种原因,当我尝试搜索列表并将字符串“selectedArtist”与event.getArtist()
方法进行比较时,它返回的内容不是列表中实际存在的项目。它实际上似乎唯一返回的项目是最后输入的列表项。
例如:
比较功能会告诉我Clown和Singer不存在,但舞者确实存在。
我认为它与我在foreach循环中的代码或者Events类的实际代码有关,但我只是不知道。
AddCustomer表单的代码
(比较在btnFind_click
)
public partial class AddCustomer : Form
{
//arrayList
List<Person> personList = new List<Person>();
List<Events> eventsList = new List<Events>();
//class
Person p = new Person();
Events ev = new Events();
//variables
String chosenArtist;
//constructors
public AddCustomer()
{
InitializeComponent();
}
public AddCustomer(List<Events> eventsList, List<Person> personList)
{
InitializeComponent();
this.personList = personList;
this.eventsList = eventsList;
}
private void btnHome_Click(object sender, EventArgs e)
{
formHome home = new formHome();
this.Hide();
home.Show();
}
private void AddCustomer_Load(object sender, EventArgs e)
{
//sets error label text to blank
lblCustomerError.Text = "";
//sets display for rich text box
rtbAddCust.Text = "Events\n";
foreach (Events ev in eventsList)
{
rtbAddCust.AppendText("Artist name: " + ev.getArtist() + "\n" +
"Event venue: " + ev.getVenue() + "\n" +
"Event date: " + ev.getDate() + "\n" +
"Capacity: " + ev.getCapacity() + "\n" +
"Price: £" + ev.getPrice() + "\n" +
"Remaining tickets: " + ev.getRemaining() + "\n\n");
}
//Ensures curser always starts at top of text box
rtbAddCust.SelectionStart = 0;
rtbAddCust.ScrollToCaret();
}
private void btnFind_Click(object sender, EventArgs e)
{
chosenArtist = txtFindArtist.Text;
foreach(Events ev in eventsList)
{
//compares chosen artist to list items to determine if present
if (ev.getArtist().ToLower().Equals(chosenArtist.ToLower()))
{
lblCustomerError.Text = "Artist found";
//do something
}
else
{
lblCustomerError.Text = "artist not found";
txtFindArtist.Clear();
}
}
}
}
事件类代码
public class Events
{
private String artist;
private String venue;
private String date;
private String capacity;
private String remaining;
private String price;
public Events() { }
public Events(String artist, String venue, String date, String capacity, String remaining, String price )
{
this.artist = artist;
this.venue = venue;
this.date = date;
this.capacity = capacity;
this.remaining = remaining;
this.price = price;
}
//setters
public void setArtist(String artist) { this.artist = artist; }
public void setVenue(String venue) { this.venue = venue; }
public void setDate(String date) { this.date = date; }
public void setCapacity(String capacity) { this.capacity = capacity; }
public void setRemaining(String remaining) { this.remaining = remaining; }
public void setPrice(String price) { this.price = price; }
//getters
public String getArtist() { return artist; }
public String getVenue() { return venue; }
public String getDate() { return date; }
public String getCapacity() { return capacity; }
public String getRemaining() { return remaining; }
public String getPrice() { return price; }
//toString
public override String ToString() { return artist + " " + venue + " " + date + " " + capacity + " " + remaining + " " + price; }
也许重要的是要注意,例如,如果在foreach
循环内我改变了我的RichTextBox内的文本,只打印出艺术家的名字,这很好。似乎只是当我试图将这些名称与我遇到问题的那些名称进行比较时。
答案 0 :(得分:1)
以下事件处理程序导致问题。检查if
,假设我输入了歌手&#39;在文本框中单击“查找”。它会将它与第一项 Clown 进行比较,导致不匹配,然后将其与歌手进行比较,这是匹配。但是你继续将它与下一个项目 Dancer 进行比较,这又是一个不匹配。找到匹配后,您需要从循环中break
:
private void btnFind_Click(object sender, EventArgs e)
{
chosenArtist = txtFindArtist.Text;
foreach(Events ev in eventsList)
{
//compares chosen artist to list items to determine if present
if (ev.getArtist().ToLower().Equals(chosenArtist.ToLower()))
{
lblCustomerError.Text = "Artist found";
//do something
break;//BREAK FROM LOOP
}
else
{
lblCustomerError.Text = "artist not found";
txtFindArtist.Clear();
}
}
}
稍微清洁一点的实现可能是这样的:
private void btnFind_Click(object sender, EventArgs e)
{
chosenArtist = txtFindArtist.Text.ToUpperInvariant;
var found=false;
foreach(Events ev in eventsList)
{
if (ev.getArtist().ToUpperInvariant().Equals(chosenArtist))
{
found=true;
break;//BREAK FROM LOOP
}
}
if(found){
lblCustomerError.Text = "Artist found";
//Do booking stuff
}
else
{
lblCustomerError.Text = "Artist not found";
txtFindArtist.Clear();
}
}
编辑: - 如@AlexiLevenkov所述,您可以利用C#中的自动属性。如果由某些C#dev编码,那么Event
类将如下所示。
公共课程活动 { public String Artist {get; set;} public String Venue {get; set;} public String Date {get; set;} public String Capacity {get; set;} public String Remaining {get; set;} public String Price {get; set;}
public Events() { }
public Events(String artist, String venue, String date, String capacity,
String remaining, String price )
{
Artist = artist;
Venue = venue;
Date = date;
Capacity = capacity;
Remaining = remaining;
Price = price;
}
public override String ToString()
{
return artist + " " + venue + " " + date + " " + capacity + " " +
remaining + " " + price;
}
}
另外,请考虑为Date
,Price
,Capacity
等采用适当的数据类型。