Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I have the following function that accepts an array of strings or an array object. I want to extend this function in such a way that it also accepts a list of objects and a list of strings.

Cannot find a way of how to implement it. Any help would be appreciated.

C#
public override void Run()
        {
            log.Trace(this, String.Format(LoggingConstants.StartCustomStep, this.GetType().Name));

            object[] realList;
            object rawList = (object)GetInput("List", typeof(object));
            object item = (object)GetInput("Item", typeof(object));

            if (rawList is object[])
            {
                realList = (object[])rawList;
            }
            else if (rawList is string)
            {
                realList = (rawList as string).Split(";".ToCharArray());
            }
            else
                throw new ArgumentException("Invalid List argument supplied.  Must be an array or string.");

            string status = StatusConstants.Fail;;
            if (item != null)
            {
                foreach (object listItem in realList)
                {
                    if (item.Equals(listItem))
                    {
                        status = StatusConstants.Pass;
                        break;
                    }
                }
            }

            SetOutput("Status", status);

            log.Trace(this, String.Format(LoggingConstants.EndCustomStep, this.GetType().Name));
        }


What I have tried:

nothing that managed to work. However someone advised to start by checking the list then use else if to check the array
Posted
Updated 1-Dec-16 23:28pm

1 solution

Have the method accept an IEnumerable<string></string> argument. Both lists and arrays implement IEnumerable, and you can still iterate through the members with foreach.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900