C# invoke code

Hi all!
I need to use a C# code in my project but I can not get it working.
I got the following error:
Framework/Dispatcher.xaml: No compiled code to run
error CS1001: Identifier expected At line 1
error CS1001: Identifier expected At line 2
error CS1513: } expected At line 2
error CS1022: Type or namespace definition, or end-of-file expected At line 155
error CS0118: ‘System’ is a namespace but is used like a type At line 1
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 1
error CS0118: ‘System.Collections.Generic’ is a namespace but is used like a type At line 2
error CS0210: You must provide an initializer in a fixed or using statement declaration At line 2

My code (to use the JaroWinkler Similarity):

using System;
using System.Collections.Generic;

public class CodeToInvoke
{
    public static string ReplaceMultipleSpaces(string inputString)
    {
        string outputString = "";
        char prevChar = '\0';

        foreach (char c in inputString)
        {
            if (c != ' ' || prevChar != ' ')
            {
                outputString += c;
            }
            prevChar = c;
        }

        return outputString;
    }

    public static double JaroSimilarity(string s1, string s2)
    {
        int lenS1 = s1.Length;
        int lenS2 = s2.Length;

        if (lenS1 == 0 || lenS2 == 0)
        {
            return 0.0;
        }

        int maxDist = Math.Max(lenS1, lenS2) / 2 - 1;
        int matchCount = 0;
        int[] s2Matches = new int[lenS2];

        for (int i = 0; i < lenS1; i++)
        {
            int start = Math.Max(0, i - maxDist);
            int end = Math.Min(i + maxDist + 1, lenS2);
            bool foundMatch = false;

            for (int j = start; j < end; j++)
            {
                if (s1[i] == s2[j] && s2Matches[j] == 0)
                {
                    matchCount++;
                    s2Matches[j] = 1;
                    foundMatch = true;
                    break;
                }
            }

            if (!foundMatch)
            {
                for (int j = start; j < end; j++)
                {
                    if (s1[i] == s2[j] && s2Matches[j] == 1)
                    {
                        s2Matches[j] = 2; // Mark as transposition
                        break;
                    }
                }
            }
        }

        if (matchCount == 0)
        {
            return 0.0;
        }

        int transpositions = 0;
        int jaroJ = 0;
        for (int i = 0; i < lenS1; i++)
        {
            if (s1[i] == s2[jaroJ])
            {
                while (s2Matches[jaroJ] != 1)
                {
                    jaroJ++;
                }
                if (s1[i] != s2[jaroJ])
                {
                    transpositions++;
                }
                jaroJ++;
            }
        }

        transpositions /= 2;

        double jaroSim = (matchCount / (double)lenS1 + matchCount / (double)lenS2 + (matchCount - transpositions) / (double)matchCount) / 3.0;

        double p = 0.1; // Winkler scaling factor
        int l = 0;
        while (l < 4 && s1[l] == s2[l])
        {
            l++;
        }

        double jaroWinklerSim = jaroSim + (l * p * (1 - jaroSim));

        return jaroWinklerSim;
    }
}

public class Result
{
    public double JaroWinklerSimilarity { get; set; }
    public string MostSimilarWord { get; set; }
}

public static Result Run(string inputWord, List<string> wordList)
{
    Result result = new Result();

    try
    {
        string inputWordCheck = CodeToInvoke.ReplaceMultipleSpaces(inputWord);

        double highestSimilarity = 0;
        string mostSimilarWord = "";

        foreach (string word in wordList)
        {
            double similarity = CodeToInvoke.JaroSimilarity(inputWordCheck, word);

            if (similarity > highestSimilarity)
            {
                highestSimilarity = similarity;
                mostSimilarWord = word;
            }
        }

        if (highestSimilarity < 0.7)
        {
            Console.WriteLine("No match found");
        }
        else
        {
            result.MostSimilarWord = mostSimilarWord;
            result.JaroWinklerSimilarity = highestSimilarity;
        }
    }
    catch (Exception ex)
    {
        // Handle exceptions here and log them if needed
        Console.WriteLine($"Error: {ex.Message}");
    }

    return result.JaroWinklerSimilarity;
}

I also want to pass 2 In arguments (string - inputWord, list of strings - wordList) and take one out argument (object - result)

Please help me get it working and tell me what should I do to use it inside InvokeCode activity.

Hi,

Unfortunately, we cannot write class in InvokeCode activity. (It behaves like single static method)

If you need to write your original class, please create/use custom activity using Visual Studio OR use coded automations in the latest 23.10 version as the following document.

https://docs.uipath.com/studio/standalone/2023.10/user-guide/coded-automations-introduction

Regards,

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.