Ryan Thomas Ryan Thomas - 5 years ago 247
Vb.net Question

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
End If

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.

Answer Source

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
   End If

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download