tags:

views:

56

answers:

2

How can I remove an item in an array?

This is what I have, which is not OK.

// fr_watchdog.items = public array
string numToRemove = "test";
fr_watchdog.items = Array.FindAll(fr_watchdog.items,
                                  val => val != numToRemove).ToArray();

All done. I have found a solution for this problem (listed below). Is it possible to delete the question?

ArrayList items = new ArrayList();

int index = Array.IndexOf(items, "Delete me");

if ( index != -1 )
{
    string[] copyStrArr = new string[items.Length - 1];
    for ( int i = 0; i < index; i++ )
    {
         copyStrArr[ i ] = items[ i ];
    }
    for ( int i = index ; i < copyStrArr.Length; i++ )
    {
         copyStrArr[ i ] = items[i + 1];
    }
}
+2  A: 

You can't "remove" an item from an array really - you can set an element's value to null (assuming it's a reference type) but arrays have a fixed size, so you can't "remove" an element any more than you can "add" one.

What you've shown in your question would create a new array - although it actually creates two arrays, because you're calling ToArray on the result, for no particular reason.

It's not even clear which ToArray method you're calling, given that you mentioned .NET 2, which doesn't include LINQ to Objects. Are you using LINQBridge? The more idiomatic LINQ way would be:

fr_watchdog.items = fr_watchdog.items.Where(val => val != numToRemove)
                                     .ToArray();

However, you've said that that's "not ok" without saying in what way it's not okay. It will create a new array populated with items where the value isn't that of numToRemove. In what way is that not what you want?

If the problem is just that the ToArray method doesn't work precisely because you're using .NET 2 without LINQBridge, then just remove the call to ToArray() from your original FindAll code. Again, note that that won't change the contents of the existing array - so any other references to that original array will still see all the original items.

Jon Skeet
LINQ get back-ported to 2.0? :)
Nick Craver
Well, that doesn’t explain why his code doesn’t work, although of course it would have been nice if he had said a bit more about the error.
Timwi
@Nick Craver: Yes, with LINQBridge. @Timwi: Precisely why I asked for more details, but also explained a few extra things along the way. If the problem is really just that he's trying to use LINQ's `ToArray` method, the simplest fix is to get rid of the call :)
Jon Skeet
@Power-Mosfet: So get rid of the call to `ToArray()`, as specified in the last paragraph of my answer. That would have been in my original answer if your question had been more specific, by the way.
Jon Skeet
@Jon - Hah I was joking, but haven't looked at at 2.0 stuff at all, all current projects are on 4.0. Nice to know there's some functionality available if a 2.0 project ever comes across my desk again :)
Nick Craver
@Nick: Another thing worth knowing is that most of the C# 3 features still work when you're targeting .NET 2. So you can still use lambda expressions, anonymous types, automatically implemented properties and even LINQ query syntax without targeting .NET 3.5.
Jon Skeet
@Jon - Excellent point - I suppose my point of view's a bit skewed, I'm using Linq2SQL/Oracle that's not supported the same way without the 3.5 libraries...I don't even think about most of the fundamental linq behaviors/features being available just via the compiler, definitely will now though.
Nick Craver
A: 

fr_watchdog.items could be a read-only property (a property without a setter) or a read-only field (a field declared with the readonly keyword). If that is the case, you cannot assign to it.

Timwi