Forum Discussion

frank_guarino_murata_com's avatar
1 year ago
Solved

How to pass a C# list variable to another C# method?

When one calls a method, generally the AML is passed. Is there a way to pass a C# variable so I won't have to regex a string that it casts into when I pass it as I do currently in an AML tag attribute.

Such like access it some other way than AML for a C# to C# calling.

Best Regards,

Frank

  • christopher_gillis's avatar
    christopher_gillis
    1 year ago

    Ah, I see. Unfortunately, no, there's no way to preserve that kind of data-typing.

    However, there are plenty of utility functions to easily convert lists to and from strings nowadays. It is extra code to handle the AML layer, but one line of

    List<string> idList = this.getProperty("selected_ids", "").Split(",").ToList();

    should be enough to handle what you're looking for. And you can use the methodology from the blog post I linked to set this "fake" selected_ids property when you call this C# method. 

9 Replies

  • Could you elaborate on your exact use case? There might be a better/alternative way of achieving the same end result.

    For example, you might be able to take advantage of setting a "fake property" as demonstrated in this blog post. 

    • angela's avatar
      angela
      Catalyst II

      NO WAY! That's an illusion...a wild fever dream. CHRIS, ARE YOU BACK???? [emoticon:f395e0ed7f2b4f71a3a75ca0fa47590c]

      • christopher_gillis's avatar
        christopher_gillis
        Community Manager

        Hi Angela, I am back! Just started again at Aras recently. Hoping to help you out in the forums after I re-familiarize myself a bit. 

    • frank_guarino_murata_com's avatar
      frank_guarino_murata_com
      Creator III

      My exact use case is a JS method that promises a C#'s method completion. The JS needs to pass it window.selectedIds. Also a C# onAfterAdd method calls the same C# method as the JS mentioned. It seems like the middleware AML turns all passed arguments into a string. Is there a way to do a more traditional object orientated caller, where I can preserve the data type?

      I understand that the AML layer and non-compatible data type of a JS list and C# list would require window.selectedIds to be passed as a string, except I would like know of a way to expect an argument as a list.

      When it comes to C# to C#, as mentioned in the paragraph before last, is there a way to have no AML in between the calls, so I can preserve a C# list to allow me to use myList.Add() right away?

      Best Regards,

      Frank

      PS Thank you for the blog post, I'll be bookmarking that

      • christopher_gillis's avatar
        christopher_gillis
        Community Manager

        Hi Frank,

        Just wanted to add onto my answer since there are a couple different ways to call a C# method from a JavaScript method. In case you weren't already doing this, I wanted to point you to the example at the end of this blog. If you have your JavaScript array of IDs, the easiest way to call your server method would be something like:

        // The type we pass in is arbitrary for method calls like this unless your logic specifically checks for it. We'll just use Part as an example
        var yourServerMethod = aras.IomInnovator.newItem("Part", "YOUR_C#_METHOD_NAME");
        yourServerMethod.setProperty("selected_ids", yourJsArray.join(','));
        yourServerMethod.apply();

  • Or to pass a referenceable variable in the AML. Such like it would have its own methods and attributes unlike having XML methods and attributes.

    • angela's avatar
      angela
      Catalyst II

      Not sure if I understand the question right. If I do: Ask your favorite AI tool to write you a function to escape problematic XML characters and pass the escaped string.

      I right now don´t remember if there wasn´t even an official solutions to pass plain AML in other AML queries,...