Ryan Thomas
Ryan Thomas

Reputation: 2022

Remove Specific Value From List In VB.NET

I have a list of values, these are all a combination of letters and numbers, to make an ID, however on some occasions this can just simply be a 0.

I need to remove all occasions where this is a 0.

I've tried something like

For i = 0 the list.count - 1
   If list(i) = "0" Then
       list.RemoveAt(j)
   End If
Next

But this then throws an argument out of range exception at the end because the loop continues, and I can't use Exit For because I need to check for multiple zeros.

I'm not very good at the Lamda expressions that seem to do what I want, and don't understand them, so if someone could give and explain one that would work, that'd be brilliant.

Upvotes: 2

Views: 25535

Answers (4)

Mike_OBrien
Mike_OBrien

Reputation: 1423

using LINQ or lambda expressions would be fairly easy solution to this problem.

The IEnumerable(Of T) interface contains a method called RemoveAll. This function can seem a bit daunting because it takes in a rather nasty looking parameter but it really isn't as bad as it looks.

So the parameter passed into RemoveAll can be an anonymous function or an address to a function. For this case an anonymous function is the way to go.

To remove all the objects from a list of strings where a condition is met you can use:

list.RemoveAll(Function(o) o="0")

This short line of code is actually doing quite a few things. First its creating an anonymous function that takes in a string as a parameter and returns the result of the comparison of that string to the literal "0". Next the RemoveAll function uses that anonymous function to evaluate all the members of the list and removes the object if the anonymous function evaluates to true.

Alternative:

The same code could be done with a function pointer. This method results in a bit more code but allows you to do a lot more in your comparison logic than just compare each value to a literal.

We still call the RemoveAll function like before but this time we pass the address of a function that has the signature similar to Public Function <FunctionName>(byval Param As String) As Boolean if the signature does not match then you will get an error.

List.RemoveAll(AddressOf TestFunction)

Next we define the function off somewhere else in the program.

Public Function TestFunction(ByVal item As String) As Boolean
    return item = "0"
End Function

Upvotes: 7

Fabio
Fabio

Reputation: 32463

Another approach can be creating new list which doesn't contain "0"-values

Dim newlist As List(Of String) = list.Where(function(v) v.Equals("0") = false).ToList()

Upvotes: 0

Ceres
Ceres

Reputation: 3668

Start from the end of the list and go backwards toward 0

For i = list.count - 1 to 0 Step -1
   If list(i) = "0" Then
       list.RemoveAt(j)
   End If
Next

If you want to use linq with a lambda use Where to keep all items that are not equal to "0" and recreate the list with ToList

list = list.Where(function(value) value <> "0").ToList

The lamda in this case is the same as

Function IsNotZero(value As String) As Boolean
    return value <> "0"
End Function

Upvotes: 8

Bradley Uffner
Bradley Uffner

Reputation: 17001

The trick is to loop backwards

For i = thelist.count - 1 to 0 step -1

Because you are going backwards you don't have to worry about shifting the items down as you go and messing up your index.

Upvotes: 2

Related Questions