Rocky
Rocky

Reputation: 4524

Unable to remove the item with .Remove

I am trying to remove the item from the arraylist but item is not getting remove, I am not getting any error remove is not working.

protected void ibtnMoveUp_Click(object sender, ImageClickEventArgs e)
    {
        ArrayList ImgArry = new ArrayList();
        ImgArry.Add(SelImgId);
        ImgArry.Add(SelImgpath);//image name
        ImgArry.Add(SelImgName);//image path
        List<int> t1 = new List<int>();
        if (Imgarry1 != null)
            t1 = Imgarry1;//Imaarry1 is the type List<int>
        t1.Add(Convert.ToInt32(ImgArry[0]));
        Imgarry1 = t1;
        List<ArrayList> t = new List<ArrayList>();
        if (newpath.Count > 0)// newpath is the type List<ArrayList> nd creating the viewstate
            t = newpath;
        t.Remove(ImgArry);//Item is not getting remove
        newpath = t;
        for (int i = 0; i < newpath.Count; i++)
        {
            ArrayList alst = newpath[i];
            newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

        }
        dlstSelectedImages.DataSource = newtb;
        DataBind();
}

Upvotes: 0

Views: 449

Answers (3)

Rocky
Rocky

Reputation: 4524

Adam Houldsworth is saying write but I have done with some different way nd my code is bellow

i have removed t.Remove(ImgArry); this line and added

             List<ArrayList> temp = new List<ArrayList>(t.Count);
                for (int i = 0; i < t.Count; i++)
                {
                    ArrayList al = t[i];
                    if (Convert.ToInt32(al[0]) != Convert.ToInt32(ImgArry[0]))
                        temp.Add(al);
                }
                t = temp;

Upvotes: 0

Seb
Seb

Reputation: 2715

ImgArry is a local variable, reference type. As the default behaviour on reference types for Equals() is indeed ReferenceEquals(), you can't achieve it, because the instance you just created can't have been put in your container in anyway.

You must search the item you wan't to remove first. E.g. : t.Find(a => a[0] == SelImgId)

Then you can t.Remove() the previously returned item.

Upvotes: 0

Adam Houldsworth
Adam Houldsworth

Reputation: 64537

Remove is working, but the item you are passing is not passing equality tests with any item in the list.

Removing by providing an object will try to test equality (usually via .Equals()) of that item with all items in the list until one is found, then it is removed. If it doesn't find any, it won't cause an exception.

Upvotes: 1

Related Questions