Tip: How to use Lambda Expressions as Sub/Function Replacements in Invoke Code Activity

Functions can generally be used to structure code sequences. However, due to the technical necessity of the Invoke Code Activity, it is not possible to apply this approach here. In this post I show how lambda expressions can be used for this.

Lambda expressions are anonymous functions that can be used anywhere in a source code. They can be defined with or without a return value. This means in VBdotNET Sub or in C# void without a return value, as lambda expression ACTION, and in VBdotNET Function or in C# a datatype with a return value, as lambda expression FUNC.

The following C# example shows six functions, three without a return value and three with a return value. In the examples only a different number of parameters are passed.

//-Lambda Expressions---------------------------------------------------
//-
//- Action = Method with return value void (Sub)
//- Func = Method with a typed return value (Function)
//-
//----------------------------------------------------------------------

//-Sub Statement--------------------------------------------------------
Action a1 = () => {
  Console.WriteLine("Hello World");
};

a1();

//-Sub Statement with String Argument-----------------------------------
Action<string> a2 = (string name) => {
  Console.WriteLine("Hello " + name);
};

a2("Stefan");

//-Sub Statement with String Arguments----------------------------------
Action<string, string> a3 = (string name, string weekday) => {
  Console.WriteLine("Hello " + name + ", how are you at " + weekday + "?");
};

a3("Stefan", "Sunday");

//-Function Statement with String Return Value--------------------------
Func<string> a4 = () => {
  return "Hello World";
};

Console.WriteLine(a4());

//-Function Statement with String Argument and Return Value-------------
Func<string, string> a5 = (string name) => {
  return "Hello " + name;
};

Console.WriteLine(a5("Hugo"));

//-Function Statement with String Arguments and Return Value------------
Func<string, string, string> a6 = (string name, string weekday) => {
  return "Hello " + name + ", how are you at " + weekday + "?";
};

Console.WriteLine(a6("Hugo", "Saturday"));

Here the same for VBdotNET.

'-Lambda Expressions----------------------------------------------------
'-
'- Action = Sub, with return value void
'- Func = Function
'-
'-----------------------------------------------------------------------

'-Sub Statement---------------------------------------------------------
Dim a1 As Action = _
Sub()
  Console.WriteLine("Hello World")
End Sub

a1

'-Sub Statement with String Argument------------------------------------
Dim a2 As Action(Of String) = _
Sub(name As String)
  Console.WriteLine("Hello " & name)
End Sub

a2("Stefan")

'-Sub Statement with String Arguments-----------------------------------
Dim a3 As Action(Of String, String) = _
Sub(name As String, weekday As String)
  Console.WriteLine("Hello " & name & ", how are you at " & weekday & "?")
End Sub

a3("Stefan", "Sunday")

'-Function Statement with String Return Value---------------------------
Dim a4 As Func(Of String) = _
Function() As String
  Return "Hello World"
End Function

Console.WriteLine(a4())

'-Function Statement with String Argument and Return Value--------------
Dim a5 As Func(Of String, String) = _
Function(name As String) As String
  Return "Hello " & name
End Function

Console.WriteLine(a5("Hugo"))

'-Function Statement with String Arguments and Return Value-------------
Dim a6 As Func(Of String, String, String) = _
Function(name As String, weekday As String) As String
  Return "Hello " & name & ", how are you at " & weekday & "?"
End Function

Console.WriteLine(a6("Hugo", "Saturday"))

As we can see, it is very easy to “replicate” functions via lambda expressions in Invoke Code Activities.

Certainly is it questionable whether code segmentation within an Invoke Code Activity makes sense. Should only small manageable code sequences be implemented within an Invoke Code Activity after all. Nevertheless, this approach can help us to make the code sequences clearer for this case. For specific use cases, this can be profitable.

10 Likes

This is really useful information, thanks @StefanSchnell!!

2 Likes

@nerlichman

Thank you very much Nicolas. :grinning:

1 Like

Thanks @StefanSchnell for informative post

1 Like

@kumar.varun2

Thank you very much Varun. :grinning:

Thanks a lot for sharing the information :grin:

1 Like

@Shubham_Varshney

Thank you very much Shubham. :grinning:

interested

1 Like

@joannadior94

Welcome in the UiPath Community Joanna.