开发者

Remove recently added item from List

开发者 https://www.devze.com 2022-12-29 04:43 出处:网络
List <Customer> collCustList = new List<Customer>(); I tried if(A==B) collCustList.Add(new Customer(99, \"H\", \"P\"));
List <Customer> collCustList = new List<Customer>();

I tried

if(A==B)
        collCustList.Add(new Customer(99, "H", "P"));

else
        collCustList.Remove(new Customer(99, "H", "P"));

but it doesn't work

How could I remove the new开发者_运维问答 item(new Customer(99, "H", "P")) I just added?

Thanks


Instead of removing the Customer instance you just added, you're trying to remove a new Customer instance. You'll need to get a reference to the first Customer instance and remove that, eg:

Customer customer = new Customer(99, "H", "P");
collCustList.Add(customer);
collCustList.Remove(customer);

or, more succinctly, if you're know you're removing the most recent customer you may do:

collCustList.Remove(collCustList.Last());

If you don't have an existing reference to the Customer instance you're trying to remove, you could use a Linq query like so:

Customer customer = collCustList.Where(c => c.Number == 99 && c.Type == "H" /* etc */).FirstOrDefault();
if (customer != null)
{
    collCustList.Remove(customer);
}

or even just use the RemoveAll() method:

collCustList.RemoveAll(c => c.Number == 99 && c.Type == "H" /* etc */);


If you want this to work, you can use List<T> and have Customer implement IEquatable<Customer>. Simple example:

using System;
using System.Collections.Generic;

class Customer : IEquatable<Customer>
{
    public int i;
    public string c1, c2;
    public Customer(int i, string c1, string c2)
    {
        this.i = i;
        this.c1 = c1;
        this.c2 = c2;
    }

    bool System.IEquatable<Customer>.Equals(Customer o)
    {
        if(o == null)
            return false;
        return this.i == o.i &&
            this.c1 == o.c1 &&
            this.c2 == o.c2;
    }

    public override bool Equals(Object o)
    {
        return o != null && 
            this.GetType() == o.GetType() &&
            this.Equals((Customer) o);
    }

    public override int GetHashCode()
    {
        return i.GetHashCode() ^
            c1.GetHashCode() ^
            c2.GetHashCode();
    }
}


new Customer() will create a new instance which is different from the one which you are adding into the List. So List will not be able to match an object for removal.

So use the way which Robert suggested or you have to iterate through the List for matching data(99, "H", "P") and then remove using matched customer object's reference.

/jay


Customer c = new Customer(99, "H", "P");
List collCustList = new List(); collCustList.Add(c);
collCustList.Remove(c);

This works because it is removing the same object that was added. Your code is trying to remove another new object that is not the same as the object you just added (it is a different object).


If you want to use Remove(), then Customer class would need to implement EqualityComparer.Default


If u want to removeat

  private void removeButton_Click( object sender, EventArgs e )
      {
        if ( displayListBox.SelectedIndex != -1 )
          displayListBox.Items.RemoveAt( displayListBox.SelectedIndex );
      }
0

精彩评论

暂无评论...
验证码 换一张
取 消