Thursday, May 22, 2014

How to replace switch statement with dictionary:

   First is a sample class with function GetStateCapital which contain switch case based value assignment. While iGetStateCapital is it’s another implementation using dictionary.

class StateCapital
    {
        /*using switch case based variable assignment*/
        public string GetStateCapital(string state)
        {
            string strCapital = string.Empty;
            switch (state.ToLower())
            {
                case "maharashtra":
                    strCapital = "mumbai";
                    break;
                case "goa":
                    strCapital = "panji";
                    break;
                case "delhi":
                    strCapital = "new delhi";
                    break;
                case "gujarat":
                    strCapital = "gandhi nagar";
                    break;
                case "ap":
                    strCapital = "hyderabad";
                    break;
                case "up":
                    strCapital = "lucknow";
                    break;
                case "mp":
                    strCapital = "bhopal";
                    break;
                case "tamilnadu":
                    strCapital = "chennai";
                    break;
                case "west bengal":
                    strCapital = "kolkota";
                    break;
            }
            return strCapital;
        }

        /*alternate implementation using dictionary*/
        public string iGetStateCapital(string state)
        {
            string capital = string.Empty;
            Dictionary<string, string> dictionary = new Dictionary<string, string>
                            {
                                {"maharashtra","mumbai"},
                                {"goa", "panji"},
                                {"delhi", "new delhi"},
                                {"gujarat", "gandhi nagar"},
                                {"ap", "hyderabad"},
                                {"up", "lucknow"},
                                {"mp","bhopal"},
                                {"tamilnadu","chennai"},
                                {"west bengal","kolkota"}

                            };

            dictionary.TryGetValue(state, out capital);
            return capital;
        }
    }

Now consider second class with function GetStateCapitalTemperature which makes function calls based on switch cases while iGetStateCapitalTemperature is using dictionary & delegates.
class CapitalTemperature
    {
        /*psudo implementation*/
        public int Maharashtra_CapitalTemperature(DateTime dt)
        {
            return 26;
        }
        public int west_bengal_CapitalTemperature(DateTime dt)
        {
            return 25;
        }
        public int tamilnadu_CapitalTemperature(DateTime dt)
        {
            return 29;
        }
        public int mp_CapitalTemperature(DateTime dt)
        {
            return 25;
        }
        public int ap_CapitalTemperature(DateTime dt)
        {
            return 28;
        }
        public int up_CapitalTemperature(DateTime dt)
        {
            return 24;
        }
        public int gujarat_CapitalTemperature(DateTime dt)
        {
            return 27;
        }
        public int delhi_CapitalTemperature(DateTime dt)
        {
            return 48;
        }
        public int goa_CapitalTemperature(DateTime dt)
        {
            return 25;
        }

        /*using switch case based function call*/
        public int GetStateCapitalTemperature(string state)
        {
            switch (state.ToLower())
            {
                case "maharashtra":
                    return Maharashtra_CapitalTemperature(DateTime.Now);
                case "goa":
                    return goa_CapitalTemperature(DateTime.Now);
                case "delhi":
                    return delhi_CapitalTemperature(DateTime.Now);
                case "gujarat":
                    return gujarat_CapitalTemperature(DateTime.Now);
                case "ap":
                    return ap_CapitalTemperature(DateTime.Now);
                case "up":
                    return up_CapitalTemperature(DateTime.Now);
                case "mp":
                    return mp_CapitalTemperature(DateTime.Now);
                case "tamilnadu":
                    return tamilnadu_CapitalTemperature(DateTime.Now);
                case "west bengal":
                    return west_bengal_CapitalTemperature(DateTime.Now);
                default:
                    return 0;
            }

        }

        /*alternate implementation using dictionary & Func deletegate*/
        public Dictionary<string, Func<DateTime, int>> CreateDictionary()
        {

            var dictionary = new Dictionary<string, Func<DateTime, int>>

                          {
                                {"maharashtra",this.Maharashtra_CapitalTemperature},
                                {"goa", this.goa_CapitalTemperature},
                                {"delhi", this.delhi_CapitalTemperature},
                                {"gujarat", this.gujarat_CapitalTemperature},
                                {"ap", this.ap_CapitalTemperature},
                                {"up", this.up_CapitalTemperature},
                                {"mp",this.mp_CapitalTemperature},
                                {"tamilnadu",this.tamilnadu_CapitalTemperature},
                                {"west bengal",this.west_bengal_CapitalTemperature}
                         };

            return dictionary;

        }
        public int iGetStateCapitalTemperature(string state)
        {
            Dictionary<string, Func<DateTime, int>> _dictionary = this.CreateDictionary();
            if (_dictionary.ContainsKey(state))
            {
                return _dictionary[state].Invoke(DateTime.Now);
            }
            return 0;
        }
    }


No comments:

Post a Comment