נעול Suggestion to improve conditions..

  • Thursday, August 09, 2012 6:33 PM
     
      Has Code

    HI ,

         I have got a situation where I am having multiple conditions as shown below.

    int year, month;
            string func()
            {
                if (year == 0)
                {
                    if (month == 0)
                    {
                        // return string message..
                    }
                    else if (month == 1)
                    { //string message..
                    }
                    else if (month > 1)
                    { //string message..
                    }
                }
    
                else  if (year == 1)
                {
                    if (month == 0)
                    { //string message..
                    }
                    else if (month == 1)
                    {  //string message..
                    }
                    else if (month > 1)
                    { //string message..
                    }
                }
                else if (year > 1)
                {
                    if (month == 0)
                    { //string message..
                    }
                    else if (month == 1)
                    {//string message..
                    }
                    else if (month > 1)
                    {//string message..
                    }
                }
               
            }

    The string message varies for each condition..any suggestions to improve the above condition..

    Thanks,

    Sanjay.

All Replies

  • Thursday, August 09, 2012 7:07 PM
     
     Answered Has Code

    Hi,

    Typically it happens when you use code to express selecting data from a "virtual" structure rather than using an actual data structure. For example here if you have a Data[3,3] array you should be able to do something such as :

            static string func(int year,int month)
            {
               string[,] Data={{"A","B","C"},{"D","E","F"},{"G","H","I"}};
               year = Math.Min(year, 2);
               month = Math.Min(month, 2);
               return Data[year, month];
            }
            public static void Main()
            {
                Console.WriteLine(func(1,4));
                Console.ReadLine();
            }


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Proposed As Answer by Manu Kapadia Friday, August 10, 2012 2:27 PM
    • Marked As Answer by msanjayv Friday, August 10, 2012 3:42 PM
    •