Invoke Code - VB.Net / C # - Condition Evaluation

Hi Everyone,

I am working on a sample workflow for my understanding in UiPath where am trying to pass a condition to test as a string argument to a script and take out the boolean result as an output.

I need help in developing or resolving the script I have in place for evaluating the expression in C# or VB.net just trying to learn and create a sample of each for my learning.

C# Code
string condition = testCondition;
out bool result;
if (bool.TryParse(condition, result))
return result;
else
// Handle more complex expressions here, e.g., using a scripting language or a dedicated expression evaluator
throw new ArgumentException(“Invalid condition”);

VB . NET
Public Function EvaluateTestCondition(testCondition As String, ByRef result As Boolean) As Boolean
Try
’ Use a more reliable expression evaluation method, such as a custom parser or a scripting language like PowerShell
’ For this example, let’s assume a simple boolean expression parser:

    Dim expressionParser As New SimpleExpressionParser()
    result = expressionParser.Evaluate(testCondition)
    Return True
Catch ex As Exception
    result = False
    Return False
End Try

End Function

Public Class SimpleExpressionParser
Public Function Evaluate(expression As String) As Boolean
’ Implement your custom expression parsing logic here
’ This is a simplified example to illustrate the concept:

    If expression.ToLower().Trim() = "true" Then
        Return True
    ElseIf expression.ToLower().Trim() = "false" Then
        Return False
    Else
        ' Handle more complex expressions as needed
        ' You can use regular expressions, custom parsing rules, or integrate with a scripting language
        Throw New NotImplementedException("Unsupported expression")
    End If
End Function

End Class

Screenshots of the issue am facing are as below
Invoke (VB.NET)


Invoke (C#)

Any help in resolving the conflicts are appreciated in advance.

Thanks and Cheers.!

@Gautham_Pattabiraman

in invoke code you need to give the code directly no need to declare classes and all…it does not support

also any input and output arguments you need are to be added usign edit arguments

think of it like this…invoke code activity itself offer the public class method and edit arguments give you the input and output arguments…so use your code inside it directly…remove any class or anything

cheers

1 Like

@Anil_G ,
Bro in C# I haven’t declared any such public class. If possible can you help fine tuning the code.

@Gautham_Pattabiraman

Here from your screentshot

For example if you want to use a if condition use it directly

if (20 > 18) 
{
  Console.WriteLine("20 is greater than 18");
}

Cheers

@Anil_G ,

I want to have that evaluated dynamically bro say for example am having a collection of conditions which might change during runtime

Like emailid should be testabc@test.com
UserName should be testuser
Or BillAmount < 33

So i want to pass the conditions directly where variables would be replaced with values they have and evaluation needs to take place

That is why trying to design this way.

@Gautham_Pattabiraman

In condition you can use arguments which you can add in edit arguments window as in argument and use

If(varA<varB)

VarA and varB to be added as in argument

Cheers

@Anil_G ,

The condition itself changes all conditions from a data collection need to be evaluated 1 by 1 . Instead of coding it inside UiPath studio which will fix condition and whenever we need to add or remove condition we have to do a code push. So the idea is to evaluate whatever is present in a remote file.

I need assistance on the script part of this solution.

Not sure how to modify and make this work.

@Gautham_Pattabiraman

ideally using invoke code you cannot acheive it

any code that you keep should be evaluated and any arguments you pass would be of any type but not a condition…like you can pass string,text, dictioanry but not the condition code itself

think of it like this say you are passing some random condtion like abc > 1 …normally when ui compiles the code it would identifiy abc and 1 are not of same type but during runtime if you want to pass this the problem is it does not even know that

cheers

@Anil_G , Thanks for the headsup.! will check on that understood the complication.

1 Like