I created a list of instances of a class, I don't know how to delete for exemple item at index 1 oPerson[1] ...

Here is my code

ViewerList[] oPerson = new ViewerList[2];
oPerson[0] = new ViewerList();
oPerson[0].DVRBrandID = "0";
oPerson[0].DVRModelID = "0";
oPerson[0].DVRViewerPath = "";
oPerson[1] = new ViewerList();
oPerson[1].DVRBrandID = "0";
oPerson[1].DVRModelID = "0";
oPerson[1].DVRViewerPath = "";

I had this issue a few times, I know it might be easier to work using an ArrayList but I want to keep all my code as is !
Thanks again

Dave

Recommended Answers

All 4 Replies

You could try setting it to null.

However, afaik .NET uses a garbage collector so it won't be free'd from the memory until' the garbage collector wants to :-)

You could try setting it to null.

However, afaik .NET uses a garbage collector so it won't be free'd from the memory until' the garbage collector wants to :-)

There must be another cleaner way... Cause as soon as this item gets deleted, the grid that is referenced to should also display proper information.
I'll way for another post :) Thanks Kekke

I'm not 100% sure about this, but if you have a 2 element list, an empty list would be the one with those 2 elements on NULL, so actually setting it to NULL would be the same as "deleting" it from the list, and if .NET has a garbage collector, then you don't have to worry about the memory.
I hope this helps you :)

I have read about this is several books, and it appears that you will need to build a method that will take the array as a parameter, move all the elements except for the one you want to delete and return the new array.
Something like this where you want to delete element [0]
oPerson =DeleteArrayElement(oPerson, 0);

private ViewerList[] DeleteArrayElement(ViewerList[] value, int index)
        {
            if (index >= value.Length )
                throw new IndexOutOfRangeException();
            
            List<ViewerList> newArray = new List<ViewerList>();
            for (int i = 0; i < value.Length; i++)
            {
                if (i != index)
                    newArray.Add(value[i]);
            }
            return newArray.ToArray();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.