1

Been googling for an hour and can't seem to find the answer. The following will remove "stupid" and prints out "hello world"

Dim arr As Variant: arr = Array("hello", "stupid", "world")
Dim newArr As Variant: newArr = Filter(arr, "stupid", False)
Debug.Print Join(newArr, " ")

What is the VB.Net equivalent for Filter? Any help would be greatly appreciated! Edit: (I'm looking for a 2.0 NET Framework solution)

2 Answers 2

2

Something like this perhaps (Assumes VB 10):

Dim arr As String() = {"hello", "stupid", "world"}

Dim filteredArray = (from s in arr
                     Where s <> "stupid"
                     Select s).ToArray()

An alternative is to use Except (which is available in .Net 3.5):

Dim words As String() = {"hello", "stupid", "world"}
Dim excludedWords As String()  = {"stupid"}

Dim filteredArray = words.Except(excludedWords).ToArray()
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Chris, thanks. Your words.Except solution works for framework 3.5. Anybody know how to do it in 2.0?
0

For .NET 2.0 you can use the Array.FindAll method, like this:

Dim arr As String() = New String() {"hello", "stupid", "world"}  
Dim newArr As String() = Array.FindAll(arr, AddressOf RemoveElements)

Using this predicate:

Private Shared Function RemoveElements(ByVal s As String) As Boolean
    Return Not s.Equals("stupid")
End Function

3 Comments

Can you use lambda functions from VB.Net in .Net 2?
@MarkJ Thanks again Mark! At least one of us is paying attention to what I'm doing.
Thanks for the solution. Seems odd to me this is easier to do in VBA or VB6 than in .NET :/. I miss the filter function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.