2

I'm required by my company to use VB.NET rather than C#. It's not really that different of course, especially under the hood.

When it comes to lambdas, the long form I find frustrating. I haven't yet found if there's any short form syntax for lamdbas in VB.NET. Does that exist?

for example look how much shorter this is

ctx.Load(list.RoleAssignments, items => items.Include(
                         item => item.Member.LoginName,
                         item => item.Member.PrincipalType,
                         item => item.Member.Title)); 

than this

ctx.Load(list.RoleAssignments, Function(rac As RoleAssignmentCollection) rac.Include( _
                                                Function(item As RoleAssignment) item.Member.LoginName, _
                                                Function(item As RoleAssignment) item.Member.PrincipalType, _
                                                Function(item As RoleAssignment) item.Member.Title))
3
  • 2
    @Dai the languages are nearly identical... and I'm not sure what's "long" about writing the word function but here we are... Commented Feb 6, 2018 at 18:51
  • 2
    After a while of VB.NET, you'll find the "shortness" of C# weird... Commented Feb 6, 2018 at 19:08
  • 2
    You can cut out more verbosity by removing the As RoleAssignment from your VB, as VB can infer the parameter types just as C# can. Then it's just a matter of Function(item) instead of item =>. (And similarly the As RoleAssignmentCollection from the outer lambda.) Commented Feb 7, 2018 at 21:53

1 Answer 1

1

Yes, a short search for VBA(^) lambda came up with Microsoft Docs Lambda Expressions. To quote:

You create lambda expressions by using the Function or Sub keyword, just as you create a standard function or subroutine. However, lambda expressions are included in a statement.

The referenced site even includes useful examples. The following code is shamelessly cut and paste from the Microsoft site: Module Module2

Sub Main()
    ' The following line will print Success, because 4 is even.
    testResult(4, Function(num) num Mod 2 = 0)
    ' The following line will print Failure, because 5 is not > 10.
    testResult(5, Function(num) num > 10)
End Sub

' Sub testResult takes two arguments, an integer value and a 
' delegate function that takes an integer as input and returns
' a boolean. 
' If the function returns True for the integer argument, Success
' is displayed.
' If the function returns False for the integer argument, Failure
' is displayed.
Sub testResult(ByVal value As Integer, ByVal fun As Func(Of Integer, Boolean))
    If fun(value) Then
        Console.WriteLine("Success")
    Else
        Console.WriteLine("Failure")
    End If
End Sub

End Module

I don't do lambdas myself, I prefer fully qualified functions and subs - after all, they also do not take up much space and enhance readability of the code.

(^) Yes, I know I typed VBA instead of VB.Net, force of habit.

Sign up to request clarification or add additional context in comments.

2 Comments

i added to my question...wondering if there's a short form in VB.NET
Edited to provide an example along the lines you have asked. However, the linked website includes all of this.

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.