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.

16 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.

Thank you very much @StefanSchnell.
Is it possible to use lambda expressions in Assing Activity?
I have some problems with it and I really don´t know if i have to include any extra nuget package.
Thank you very much one more time!

@DavidCnx

Hello David,
welcome in the UiPath Community.
Nope, it seems not possible to use Lambda Expression inside an Assign Activity.
The use of Lambda Expressions does not require an additional NuGet package.
Best regards
Stefan

Thank you @StefanSchnell , so I will use invoke code in stead.

1 Like

Thanks for info, it is very useful.

But how can we define Function with string array param??

Function(arrayName As Array) or smt different??

And also can you share the workflow if it is possible. My Invoke code gets me an error.

Thanks.

Use String() if you want an array of string.

Dim ArrayToString As Func(Of String(), String) = _
Function(arr As String()) As String
  Return String.Join("#", arr)
End Function

Dim s = ArrayToString({"Hello", "World", "!"})

Console.WriteLine(s)

@StefanSchnell This is great info!! One quick question, in the function declaration in which position the return type is expected. For example if the function takes String, String and output is integer then what is the order be like?

Func<string, string, int> a6 = (string name, string weekday) => {
  return 1;
};

or 

Func<int,string, string> a6 = (string name, string weekday) => {
  return 1;
};