tags:

views:

1286

answers:

3

Is it possible to create an inline delegate in vb.net like you can in c#?

For example, I would like to be able to do something inline like this:

myObjects.RemoveAll(delegate (MyObject m) { return m.X >= 10; });

only in VB and without having to do something like this

myObjects.RemoveAll(AddressOf GreaterOrEqaulToTen) 

Private Function GreaterOrEqaulToTen(ByVal m as MyObject)
    If m.x >= 10 Then 
         Return true
    Else
         Return False
    End If
End Function

-- edit -- I should have mentioned that I am still working in .net 2.0 so I won't be able to use lambdas.

+2  A: 

Try:

myObjects.RemoveAll(Function(m) m.X >= 10)

This works in 3.5, not sure about the 2.0 syntax.

Shawn Simon
Bugger! I was too slow.. :(
BlackMael
indeed but your superior type casting trumps me
Shawn Simon
Strictly speaking it isn't required but for myself, it is a little more readable since I don't have to think about what "m" is
BlackMael
+6  A: 
myObjects.RemoveAll(Function(m As MyObject) m.X >= 10)

See Lambda Expressions on MSDN

BlackMael
A: 

Yeah, This example is really good. But Will I get some of the exercise questions in Delegates?