Sormita Chakraborty
Sormita Chakraborty

Reputation: 1055

Writing Generic method for Enum parser

I have two methods which are doing similar function. I want to merge these two into one so that the code is not repetitive.

First method:

private string GetPlanogramStatusListString()
        {
            string outValue = null;
            if (this.POGSelectedStatusList != null)
            {
                outValue = "";
                // Parse the string list to get enum values into a list
                List<string> tempStatusList = new List<string>();
                foreach (object sts in this.POGSelectedStatusList)
                {                    
                    PogStoreData.PlanogramStatusCode enumValue;
                    if (Enum.TryParse<PogStoreData.PlanogramStatusCode>(sts.ToString(), out enumValue))
                    {
                        tempStatusList.Add(((int)enumValue).ToString());
                    }
                }

                outValue = string.Join(",", tempStatusList);
            }
            return outValue;

        }

Second method:

private string GetMoveStatusListString()
        {
            string outValue = null;
            if (this.POGMovedStatus != null)
            {
                outValue = "";
                // Parse the string list to get enum values into a list
                List<string> tempStatusList = new List<string>();
                foreach (object sts in this.POGMovedStatus)
                {
                    PogStoreData.POGMovedStatus enumValue;
                    if (Enum.TryParse<PogStoreData.POGMovedStatus>(sts.ToString(), out enumValue))
                    {
                        tempStatusList.Add(((int)enumValue).ToString());
                    }
                }

                outValue = string.Join(",", tempStatusList);
            }
            return outValue;

        }

I want to merge these two methods to one single method. How can I do this?

Upvotes: 0

Views: 58

Answers (5)

PMBjornerud
PMBjornerud

Reputation: 880

Since converting a list of objects into a list of integer strings is not tied directly to a class one could use an extension method for this kind of logic. Although the benefit is limited with object types.

Also, what you are doing here is actually Map/Filter/Reduce. That means you could shorten the code slightly and use the standard LINQ names for these operations. (Map/Filter/Reduce translates to .Net Select/Where/Aggregate)

        public static string ToIntegers<T>(this IEnumerable<object> values) where T : struct
        {
            return values?.Select(x => {
                T enumValue;
                return Enum.TryParse(x.ToString(), out enumValue) ? Convert.ToInt32(enumValue).ToString() : null;
            })
            .Where(x => x != null)
            .Aggregate((s1, s2) => s1 + "," + s2);
        }

Where LINQ crosses the line between convenient and unreadable is a matter of personal taste. For people used to "Map/Filter/Reduce" this may be very easy to read, for others not so. I just wanted to give some other input since the current answers are very similar.

Upvotes: 1

John Alexiou
John Alexiou

Reputation: 29244

A single line of code would suffice:

string GetStatusList(IEnumerable<object> array) 
{
    return string.Join(",", array.Select((e)=>(Convert.ToInt32(e)).ToString()) );
}

to be used as

// GetPlanogramStatusListString();
var x=GetStatusList(POGSelectedStatusList.ToArray());
// 1,2,2

// GetMoveStatusListString();
var y=GetStatusList(POGMovedStatus.ToArray());
// 2,3,2,1,2

Upvotes: 0

neo
neo

Reputation: 425

You can use something similar to below code,

        private string GetMoveStatusListString<T>(IEnumerable list) where T : struct 
        {
            string outValue = null;
            if (list != null)
            {
                outValue = "";
                // Parse the string list to get enum values into a list
                List<string> tempStatusList = new List<string>();
                foreach (object sts in list)
                {
                    T enumValue;
                    if (Enum.TryParse<T>(sts.ToString(), out enumValue))
                    {
                        tempStatusList.Add(enumValue.ToString());
                    }
                }

                outValue = string.Join(",", tempStatusList);
            }
            return outValue;
        }

Upvotes: 0

Richard Irons
Richard Irons

Reputation: 1473

I've actually compiled and run this code:

private string GetEnumListString<T>(IEnumerable list) where T : struct
{
    string outValue = null;
    if (list != null)
    {
        outValue = "";
        // Parse the string list to get enum values into a list
        List<string> tempStatusList = new List<string>();
        foreach (object sts in list)
        {                    
            T enumValue;
            if (Enum.TryParse(sts.ToString(), out enumValue))
            {
                tempStatusList.Add((Convert.ToInt32(enumValue)).ToString());
            }
        }
        outValue = string.Join(",", tempStatusList);
    }
    return outValue;
}

private string GetPlanogramStatusListString()
{
    return GetEnumListString<PogStoreData.PlanogramStatusCode>(this.POGSelectedStatusList);
}

private string GetMoveStatusListString()
{
    return GetEnumListString<PogStoreData.POGMovedStatus>(this.POGMovedStatus);
}

It compiles, and produces identical output to the original code.

Upvotes: 1

Boomit
Boomit

Reputation: 187

Something like this?

private string GetsListString<T>(List<object> list)
{
    string outValue = null;
    if (list != null)
    {
        outValue = "";
        // Parse the string list to get enum values into a list
        List<string> tempStatusList = new List<string>();
        foreach (object sts in list)
        {
            T enumValue;
            if (Enum.TryParse<T>(sts.ToString(), out enumValue))
            {
                tempStatusList.Add(((int)enumValue).ToString());
            }
        }

        outValue = string.Join(",", tempStatusList);
    }
    return outValue;
}

Upvotes: 0

Related Questions