fredag 23. oktober 2009

Delegates and anonymous methods (by PEtter)

A little stange thing I found using delegates in vb .net 3.5

Starting with:



Public delegate Function Something(ByVal a as integer, ByVal b as integer) as boolean

Public Function doSomething(ByVal a as integer, ByVal b as integer) as boolean
  return a = b
end function



To use this you would do something like this:


 Public Function ExecuteDelegate(ByVal a As Integer, ByVal B As Integer, ByVal d As Something) As Boolean
            Return d(a, B)
        End Function


And then you can use your function with a delegate:


  ExecuteDelegate(1,2, addressOf doSomething)


In .net 3.5(vb) you could simplify this by using inline functions:


ExecuteDelegate(1,2,Function(ByVal a As Integer, ByVal B As Integer) A = B))



And so to the strange part.
If you do not need the parameters to resolve your return value you could do:


ExecuteDelegate(1,2,Function() false )


As you can see, inlining the delegate without the correct signature, but as long as you provide 'no signature' this works fine!

'Funny'?

0 kommentarer:

Legg inn en kommentar