4

Could some one please show me how I can rewrite the below method in a better and more elegant way?

// in class------------------

    public static void RefreshAllDropdownlists(DropDownList ddlRemoveUsersFromRole, DropDownList ddlAddUsersToRole, DropDownList ddlAddAllUsersToRole, DropDownList ddlRemoveAllUsersFromRole, DropDownList ddlDeleteAllUsersFromRole)
    {
        ddlRemoveUsersFromRole.ClearSelection();
        ddlAddUsersToRole.ClearSelection();
        ddlAddAllUsersToRole.ClearSelection();
        ddlRemoveAllUsersFromRole.ClearSelection();
        ddlDeleteAllUsersFromRole.ClearSelection();
    }

// in codebehind------------------

UserGvUtil.RefreshAllDropdownlists(ddlRemoveUsersFromRole, ddlAddUsersToRole, ddlAddAllUsersToRole, ddlRemoveAllUsersFromRole, ddlDeleteAllUsersFromRole);

Thank you!

2
  • 1
    Looks pretty good to me. What would you gain by rewriting the code? It does what you want. Commented Jul 18, 2010 at 23:03
  • 1
    @Alastair Pitts, with that code, the method will have to be modified every time he wants to refresh another DDL... Commented Jul 18, 2010 at 23:18

4 Answers 4

7

Use the params parameter modifier to pass an array of DropDownLists :

public static void RefreshAllDropdownlists(params DropDownList[] dropDownLists)
{
    foreach (DropDownList ddl in dropDownLists)
    {
        ddl.ClearSelection();
    }
}

Usage is is the same as with your current method

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thank you! This is exactly what I was aiming for but could not figure out the syntax.
2
var listsToRefresh = new List<DropDownList>
                {
                    ddlRemoveUsersFromRole,
                    ddlAddUsersToRole,
                    ddlAddAllUsersToRole,
                    ddlRemoveAllUsersFromRole,
                    ddlDeleteAllUsersFromRole
                };
listsToRefresh.ForEach(l=>l.ClearSelection());

There are many ways to do this, I would prefer this one. If all you are doing is performing ClearSelection() on each one, then there is no need to create a method for that one line of code. However, if you want to do some more work on each DropDownlist, then I think the use of an extension method would keep it elegant.

 public static class DropDownListExtensions
    {
        public static void Reset(this DropDownList dropDownList)
        {
            dropDownList.ClearSelection();
            //... do more stuff

        }
    }

listsToRefresh.ForEach(l=>l.Reset());

Comments

1

Something like

public static void RefreshAllDropdownlists(params DropDownList[] dropDownLists)
{
    if (dropDownLists != null)
        foreach (var ddl in dropDownLists)
            ddl.ClearSelection();
}

?

Comments

1

You could pass a list of DropDownList objects to the function, then you could do:

public static void RefreshAllDropdownlists(List<DropDownList> lists)
{
   foreach(DropDownList dropDown in lists)
   {
     dropDown.ClearSelection();
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.