Reputation: 23
We are working on a school assignment where we are supposed to do the page which must be able to show a full list of all the customers and also be able to show a list of the names of all from Jutland, a list of the names of all customers from Funen and a list of the names of all customers from Zealand.
Problem:
Instead of showing customers that are specific to one region (e.g Jutland), the system will add both the previous and the new customer (who is from a different region) into the same field, which represents the different region. Thus, the system cannot make a distinction between the individual regions of the customers.
In the screenshot above, the name Stanislav is supposed to be the only one in the ListBox
. However, Sebastian, who belongs to another region, hence the other list box, is added as well. We believe it might have something to do with the foreach
loop, although we are not entirely sure.
Any assistance will be greatly appreciated!
namespace _2ndHandinCsharp
{
public partial class register : System.Web.UI.Page
{
private CustomerTank thetank;
protected void Page_Load(object sender, EventArgs e)
{
if (Application["SharedCustomerTank"]== null)
{
Application["SharedCustomerTank"] = new CustomerTank();
}
thetank = (CustomerTank)Application["SharedCustomerTank"];
}
protected void ButtonUpdate_Click(object sender, EventArgs e)
{
thetank = (CustomerTank)Application["SharedCustomerTank"];
UpdateCustomerListView();
}
private void UpdateCustomerListView()
{
ListBox1.Items.Clear();
List<Customer> customerInTheTank = thetank.TheCustomers();
foreach(Customer c in customerInTheTank)
{
ListBox1.Items.Add(c.ToString());
}
}
private void UpdateCustomerListViewJutland()
{
List<Customer> customerInTheTank = thetank.TheCustomers();
for (int i = 0; i < customerInTheTank.Count; i++)
{
if (DropDownListRegion.SelectedValue == "Jutland")
{
ListBoxJutland.Items.Add(customerInTheTank[i].Name);
}
}
}
private void UpdateCustomerListViewFunen()
{
List<Customer> customerInTheTank = thetank.TheCustomers();
for (int i=0; i<customerInTheTank.Count;i++)
{
if (DropDownListRegion.SelectedValue == "Funen")
{
ListBoxFunen.Items.Add(customerInTheTank[i].Name);
}
}
}
protected void ButtonAddCustomer_Click(object sender, EventArgs e)
{
Customer c = new Customer(
TextBoxName.Text,
TextBoxPassword.Text,
int.Parse(TextBoxAge.Text),
TextBoxZip.Text,
DropDownListRegion.Text);
Application.Lock();
thetank = (CustomerTank)Application["SharedCustomerTank"];
thetank.AddCustomer(c);
Application["SharedCustomerTank"] = thetank;
Application.UnLock();
UpdateCustomerListView();
UpdateCustomerListViewJutland();
UpdateCustomerListViewFunen();
}
}
}`
Upvotes: 2
Views: 56
Reputation: 8726
Looks like you're using a wrong condition:
Instead of this,
for (int i = 0; i < customerInTheTank.Count; i++)
{
if (DropDownListRegion.SelectedValue == "Jutland")
{
ListBoxJutland.Items.Add(customerInTheTank[i].Name);
}
}
you should rather look at the actual location of the customer, not at the current selection in the region box:
ListBoxJutland.Items.Clear();
for (int i = 0; i < customerInTheTank.Count; i++)
{
if (customerInTheTank[i].Region == "Jutland")
{
ListBoxJutland.Items.Add(customerInTheTank[i].Name);
}
}
The name of the region property on the customer class is not included in your code, I assumed Region
.
Note that I added a Clear()
statement before the loop, which will prevent customer names from being added again and again, if the function is called repeatedly.
Upvotes: 2
Reputation: 125
You're right. The problem is with your for-each loops. When you're checking to see whether the region is/isn't Funen or Jutland you're forgetting to check whether the customer you're adding is also from that region. You just need an additional IF statement condition. Something like this should do
for (int i=0; i<customerInTheTank.Count;i++)
{
if (DropDownListRegion.SelectedValue == "Funen" && customerInTheTank[i].Region == "Funen")
{
ListBoxFunen.Items.Add(customerInTheTank[i].Name);
}
}
Upvotes: 1