Need help using vb net code in Invoke Code Activity

Hi Guys,
I’m new to UiPath.
Can you please help me convert this following code to usable code in Invoke Code activity where arrays and list will be dynamic and I will pass it as arguments.

This code is working in online compiler but pasting same in invoke code giving list of variaous errors

Thanks

Imports System
Imports System.Collections.Generic
Imports System.Linq

Public Class Program
    Dim firstArray As String() = {"Durable Medical Equipment", "Durable Medical Supplies"}
    Dim secondArray As String() = {"STANDARD SAVINGS", "Customer Non-Designated Providers"}
    Dim thirdArray As String() = {"Office"}

    Function ContainsAnyWord(str As String, words As String()) As String
        For Each word In words
            If str.Contains(word) Then
                Return word
            End If
        Next
        Return Nothing
    End Function

    Function FilterMyList(myList As List(Of String)) As Tuple(Of String, Boolean, String)
        Dim matchedWords As New List(Of Tuple(Of String, String))()
        Dim filteredList As List(Of String) = myList.Where(Function(item)
                                                               Dim match As String = ContainsAnyWord(item, firstArray)
                                                               If match IsNot Nothing Then
                                                                   matchedWords.Add(Tuple.Create(item, match))
                                                                   Return True
                                                               End If
                                                               Return False
                                                           End Function).ToList()

        Dim multipleFirstArrayMatches As Boolean = filteredList.Count > 1
        Dim matchedWord As String = If(matchedWords.Count > 0, matchedWords(0).Item2, Nothing)

        If multipleFirstArrayMatches Then
            filteredList = filteredList.Where(Function(item) ContainsAnyWord(item, secondArray) IsNot Nothing).ToList()
        End If

        filteredList = filteredList.Where(Function(item) ContainsAnyWord(item, thirdArray) Is Nothing).ToList()

        Dim filteredString As String = If(filteredList.Count = 1, filteredList(0), Nothing)
        multipleFirstArrayMatches = If(filteredString IsNot Nothing, False, multipleFirstArrayMatches)

        Return Tuple.Create(filteredString, multipleFirstArrayMatches, matchedWord)
    End Function

    Public Shared Sub Main()
        Dim program As New Program()

        Dim myListType1 As New List(Of String) From {
            "$150 Family STANDARD PLUS SAVINGS Customer Affiliated Providers Durable Medical Equipment,COPAY INCLUDED IN OOP",
            "$30 Family MAXIMUM SAVINGS Customer Designated Providers Durable Medical Equipment,COPAY INCLUDED IN OOP",
            "$0 Family STANDARD SAVINGS Customer Non-Designated Providers Durable Medical Supplies"
        }

        Dim myListType2 As New List(Of String) From {
            "$0 Individual Home Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP",
            "$0 Individual Office Continous Glucose Monitors,Continous Glucose Suppliers,Copay Included In CATAS OOP"
        }

        Dim myListType3 As New List(Of String) From {
            "$0 Individual Home Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP",
            "$0 Individual Office Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP"
        }

        Dim result1 = program.FilterMyList(myListType1)
        Dim result2 = program.FilterMyList(myListType2)
        Dim result3 = program.FilterMyList(myListType3)

        Console.WriteLine(String.Format("Result 1: {0}, Multiple Matches: {1}, Matched Word: {2}", result1.Item1, result1.Item2, result1.Item3))
        Console.WriteLine(String.Format("Result 2: {0}, Multiple Matches: {1}, Matched Word: {2}", result2.Item1, result2.Item2, result2.Item3))
        Console.WriteLine(String.Format("Result 3: {0}, Multiple Matches: {1}, Matched Word: {2}", result3.Item1, result3.Item2, result3.Item3))
    End Sub
End Class

@shital.sahare
Please let us know the overall goal of what is to be achieved along with some sample data.

In general, we could model it more simple and compact, without too much black-boxing it into the invoke code

  • we would remove the imports from code, and handle it within the imports panel
  • we cannot define a class, as invoke code is more about scripting

Hi Peter,

The main goal of this code is to filter the list of strings (myList) based on the words contained in three arrays: firstArray, secondArray, and thirdArray.

First, it filters the list based on words from firstArray. If multiple strings match using words from firstArray, it further filters them using words from secondArray. Additionally, any strings containing words from thirdArray are excluded from the final filtered list.

The code returns the filtered string (a single string), a flag indicating whether duplicates were found during the filtering process, and the word used to match and filter the strings.

please share samples, so we can better help

Have a preview look here:

No matchers:
grafik

With matchers
grafik

We can combine Intersect and Except

    firstArray = {"Durable Medical Equipment", "Durable Medical Supplies"}
    secondArray= {"STANDARD SAVINGS", "Customer Non-Designated Providers"}
    thirdArray= {"Office"}

    Various types of myList
    Type 1:
    myList = {
            "$150 Family STANDARD PLUS SAVINGS Customer Affiliated Providers Durable Medical Equipment,COPAY INCLUDED IN OOP",
            "$30 Family MAXIMUM SAVINGS Customer Designated Providers Durable Medical Equipment,COPAY INCLUDED IN OOP",
            "$0 Family STANDARD SAVINGS Customer Non-Designated Providers Durable Medical Supplies"
            }

    Type 2:
    myList = {
            "$0 Individual Home Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP",
            "$0 Individual Office Continous Glucose Monitors,Continous Glucose Suppliers,Copay Included In CATAS OOP"
            }
            
    Type 3:
    myList = {
            "$0 Individual Home Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP",
            "$0 Individual Office Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP"
            }

    expected output:
    Type 1:
    "$0 Family STANDARD SAVINGS Customer Non-Designated Providers Durable Medical Supplies"

    Type 2:
    "$0 Individual Home Durable Medical Equipment,Durable Medical Supplies,Copay Included In CATAS OOP"

just check the above given example where intersect and except was combined. Can be fast done within the immediate panel

Yes, Thanks.
Will let you know if worked or any issue.

as far we have understood

intersect l1,l2 -then remove with Except l3
grafik

then we can do the filtering on keywords with a string contains

Kindly note:

    firstArray = {"Durable Medical Equipment", "Durable Medical Supplies"}
    secondArray= {"STANDARD SAVINGS", "Customer Non-Designated Providers"}
    thirdArray= {"Office"}

l1,l2 doesn have a common item, we added one into the demo samples