Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Have a dynamic parameter for a List in Function

I have a SOAP APi which needs an XML string created to send as the request.
There are a number of different XML strings required.

eg. the Customer and Item strings are quite different and there is no WDSL ability on this old software.

I have a function to check which API enpoint is being called, then get the action, uRL and envelope (XML) from predefined functions.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private static (string URL, string action, string envelope) getTaskDetails(string task, 
List<Object> envelopeList)
        {
            string _action = "";
            string _URL = "";
            string _envelope = "";


            switch (task)
            {
                case "getCustomerDetails":
                    _action = minfosDetails.minfosCustomerAction;
                    _URL = minfosDetails.minfosCustomerURL;
                    _envelope = getCustomerDetails(envelopeList);
                    break;

                default:
                    
                    break;

            }

            return (_URL, _action, _envelope);
        }

The aobve is the (not working) code.

What I want is the List (which I know is wrong) to accept any LIST thrown at it.

eg. I can send the customerList to it, or the ItemList or the staffList etc.
Then when i run the switch, it will take the List and throw it into the subfunction to get the envolope created.

EDIT: Question answered, my working code below:
Using

getTaskDetails<T>(string task, List<T> envelopeList)

To call the next function I used:

_envelope = getCustomerDetails(envelopeList.Cast<ACustomer>());

>Solution :

You can try using generics so the method is going to change like this

private static (string URL, string action, string envelope) getTaskDetails<T>(List<T> envelopeList)

then you can check for what T Type is being passed instead of using your parameter string task

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading