locked
Convert Multipe if conditions to generic approach RRS feed

  • Question

  • Hi Folks,

     

    how to convert the below code to generic Approach with best practices.

     

                         string[] roles = Roles.GetRolesForUser(userEmail);
                        if (roles != null)
                        {
                            if (roles.Contains("DU_1") || roles.Contains("DP_1") || roles.Contains("DE_1"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("OS Screens", "1"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("OS Screens", "1"));
                            }
                            if (roles.Contains("DU_2") || roles.Contains("DP_2") || roles.Contains("DE_2"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("Apps", "2"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("Apps", "2"));
                            }
                            if (roles.Contains("DU_3") || roles.Contains("DP_3") || roles.Contains("DE_3"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("Devices", "3"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("Devices", "3"));
                            }
                            if (roles.Contains("DU_4") || roles.Contains("DP_4") || roles.Contains("DE_4"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("Brand ID", "4"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("Brand ID", "4"));
                            }
                            if (roles.Contains("DU_5") || roles.Contains("DP_5") || roles.Contains("DE_5"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("Final Creative", "5"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("Final Creative", "5"));
                            }
                            if (roles.Contains("DU_6") || roles.Contains("DP_6") || roles.Contains("DE_6"))
                            {
                                dropDownMostRecentGroup.Items.Add(new ListItem("Document Library", "6"));
                                dropDownFeaturedGroup.Items.Add(new ListItem("Document Library", "6"));
                            }
                        }
    



    Sudhesh. G
    http://gurucoders.blogspot.com
    Friday, January 27, 2012 8:29 AM

Answers

  • Hi,

    You could also consider using a data structure rather than code. Basically your code just describe associations between roles and items. It could be expressed as a data structure...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, January 27, 2012 9:57 AM
  • I see that roles is an array of strings,then how come you are able to use the Contains() method which is not applicable for Arrays.?

    As long as you include a 'using System.Linq;' directive in your code, you can use Contains on arrays, as well as all Enumerable extension methods.

    Monday, January 30, 2012 2:48 PM
  • Something like this might do what you want:

       string[] groups = new string[] {"", "OS Screens", "Apps", "Devices", "Brand ID", "Final Creative", "Document Library"};
       string[] roles = Roles.GetRolesForUser(userEmail);
       int number = GetNumberFrom(roles); //TODO write function to extract number
       dropDownMostRecentGroup.Items.Add(new ListItem(groups[number], number.ToString()));
       dropDownFeaturedGroup.Items.Add(new ListItem(groups[number], number.ToString())));

    Not the best way but pretty simple - might have syntax errors just typed in.

    PS It occurred to me that there might be more than one number in userEmail -no idea since I don't know what sort of thing it is. If so then something like this might do it:

       string[] groups = new string[] {"", "OS Screens", "Apps", "Devices", "Brand ID", "Final Creative", "Document Library"};
       string[] roles = Roles.GetRolesForUser(userEmail);
       int[] numbers = GetNumberFrom(roles); //TODO write function to extract numbers
        for(int i =0, i< numbers.Length(); i++) {
          dropDownMostRecentGroup.Items.Add(new ListItem(groups[numbers[i]], numbers[i].ToString()));
          dropDownFeaturedGroup.Items.Add(new ListItem(groups[numbers[i]], numbers[i].ToString())));
        }


    Regards David R
    ---------------------------------------------------------------
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    • Proposed as answer by Mansoor Omrani Saturday, January 28, 2012 12:31 PM
    • Edited by Riced Saturday, January 28, 2012 12:43 PM Added PS
    • Marked as answer by Dummy yoyoModerator Monday, February 6, 2012 6:06 AM
    Friday, January 27, 2012 3:40 PM

All replies

  • I see that roles is an array of strings,then how come you are able to use the Contains() method which is not applicable for Arrays.?

    Thanks, Satish Bommideni
    Friday, January 27, 2012 8:48 AM
    • Edited by Sudhesh.G Friday, January 27, 2012 9:19 AM
    Friday, January 27, 2012 9:06 AM
  • hi spartan,

    yes, we can acheive the contains method in string array,

    FYI

    http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array


    I am afraid,I don't see anything in the link where the Contains method is used for an Array.All that was used in the link was the Contains method of a string variable..

    The string array was looped to access each element in the string array which is obviously a string so that they could use the contains method on string object(i.e element of string array,not the array itself Mind you...!)

    Please let me know, if I am missing anything.


    Thanks, Satish Bommideni
    • Edited by Spartan_578 Friday, January 27, 2012 9:29 AM
    Friday, January 27, 2012 9:21 AM
  • it's a  string Array not Array variable.
    Sudhesh. G
    http://gurucoders.blogspot.com
    Friday, January 27, 2012 9:24 AM
  • Hi,

    You could also consider using a data structure rather than code. Basically your code just describe associations between roles and items. It could be expressed as a data structure...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, January 27, 2012 9:57 AM
  • Something like this might do what you want:

       string[] groups = new string[] {"", "OS Screens", "Apps", "Devices", "Brand ID", "Final Creative", "Document Library"};
       string[] roles = Roles.GetRolesForUser(userEmail);
       int number = GetNumberFrom(roles); //TODO write function to extract number
       dropDownMostRecentGroup.Items.Add(new ListItem(groups[number], number.ToString()));
       dropDownFeaturedGroup.Items.Add(new ListItem(groups[number], number.ToString())));

    Not the best way but pretty simple - might have syntax errors just typed in.

    PS It occurred to me that there might be more than one number in userEmail -no idea since I don't know what sort of thing it is. If so then something like this might do it:

       string[] groups = new string[] {"", "OS Screens", "Apps", "Devices", "Brand ID", "Final Creative", "Document Library"};
       string[] roles = Roles.GetRolesForUser(userEmail);
       int[] numbers = GetNumberFrom(roles); //TODO write function to extract numbers
        for(int i =0, i< numbers.Length(); i++) {
          dropDownMostRecentGroup.Items.Add(new ListItem(groups[numbers[i]], numbers[i].ToString()));
          dropDownFeaturedGroup.Items.Add(new ListItem(groups[numbers[i]], numbers[i].ToString())));
        }


    Regards David R
    ---------------------------------------------------------------
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    • Proposed as answer by Mansoor Omrani Saturday, January 28, 2012 12:31 PM
    • Edited by Riced Saturday, January 28, 2012 12:43 PM Added PS
    • Marked as answer by Dummy yoyoModerator Monday, February 6, 2012 6:06 AM
    Friday, January 27, 2012 3:40 PM
  • I see that roles is an array of strings,then how come you are able to use the Contains() method which is not applicable for Arrays.?

    As long as you include a 'using System.Linq;' directive in your code, you can use Contains on arrays, as well as all Enumerable extension methods.

    Monday, January 30, 2012 2:48 PM
  • Hi Sudhesh,

    Welcome to the MSDN forum!

    How is it going with the problem currently?

    If you need any help, please feel free to let us know.

    Have a nice day!

    yoyo

     


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 31, 2012 5:22 AM
    Moderator