The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Removing duplication

Hi,

Is there a better way to do this piece of code so to remove the current duplication?

private void BindComboBox2(int categoryID, string radComboBoxID)
    {
            categoryLoadedCount = CartObject.Catalog.Categories.LoadDB(categoryID);
            
            
            switch(radComboBoxID)
            {
                case "RadComboBox1":
                    RadComboBox2.HideCombo = false;
                    RadComboBox2.DataSource = CartObject.Catalog.Categories;
                    RadComboBox2.DataTextField = "Name";
                    RadComboBox2.DataValueField = "ID";
                    RadComboBox2.DataBind();
                    break;
                case "RadComboBox2":
                    RadComboBox3.HideCombo = false;
                    RadComboBox3.DataSource = CartObject.Catalog.Categories;
                    RadComboBox3.DataTextField = "Name";
                    RadComboBox3.DataValueField = "ID";
                    RadComboBox3.DataBind();
                    break;
                default:
                    
                    break;
            }

        

        }

The RadComboBox in the switch statement is fed in as a string, is it possible some how to get a reference to the RadComboBox somehow, for example RadComboBox + radComboBoxID and then use this to save duplicating the code for each radComboBox.

Many Thanks
Kieran
Kieran
Wednesday, May 24, 2006
 
 
Well, the first step is to have a variable of type ComboBox, which you assign to point to the 'next' combo box as appropriate to the current setting of radComboBoxID; then do all the setup stuff through this variable.

That takes care of only having the setup stuff in the code once.

The next question is how to assign nextComboBox. You could do it with a switch on radComboBox, pretty much as at present. Or you could make it so that each combobox knows its 'next', either in a custom property you define by deriving from the built in ComboBox, or (lazier, quicker, less correct) just shoving it in the Tag.
Larry Lard Send private email
Wednesday, May 24, 2006
 
 
Larry, I'm not sure what you mean in the first paragraph?

Thanks
Kieran
Kieran
Wednesday, May 24, 2006
 
 
> Is there a better way to do this piece of code so to remove the current duplication?

static void show(RadComboBox radComboBox)
{
 radComboBox.HideCombo = false;
 radComboBox.DataSource = CartObject.Catalog.Categories;
 radComboBox.DataTextField = "Name";
 radComboBox.DataValueField = "ID";
 radComboBox.DataBind();
}

private void BindComboBox2(int categoryID, string radComboBoxID)
{
 switch(radComboBoxID)
 {
 case "RadComboBox1":
  show(RadComboBox2);
  break;
 case "RadComboBox2":
  show(RadComboBox3);
  break;
 }
}


> The RadComboBox in the switch statement is fed in as a string, is it possible some how to get a reference to the RadComboBox somehow, for example RadComboBox + radComboBoxID and then use this to save duplicating the code for each radComboBox.

The code above removes the duplication by moving duplicated code into a common subroutine. It still uses a switch statement to convert the input string to a reference (alternatives to a switch might be to use the reflection APIs, or to have a map whose key is the string and whose value is a reference to the corresponding box).
Christopher Wells Send private email
Wednesday, May 24, 2006
 
 
> Larry, I'm not sure what you mean in the first paragraph?

private void BindComboBox2(int categoryID, string radComboBoxID)
{
 RadComboBox radComboBox;
 switch(radComboBoxID)
 {
 case "RadComboBox1":
  radComboBox = RadComboBox2;
  break;
 case "RadComboBox2":
  radComboBox = RadComboBox3;
  break;
 default:
  return;
 }
 radComboBox.HideCombo = false;
 radComboBox.DataSource = CartObject.Catalog.Categories;
 radComboBox.DataTextField = "Name";
 radComboBox.DataValueField = "ID";
 radComboBox.DataBind();
}
Christopher Wells Send private email
Wednesday, May 24, 2006
 
 
Ahhh! I knew there was a better way, just couldn't figure out how to build up the reference to the radcombobox by using the id fed in. I see what you do is use the switch to pass the radcombobox object itself back into the method.

With something like this, should I be looking to create classes to hold this type of functionality?

Thanks for your input
Kieran
Kieran
Wednesday, May 24, 2006
 
 
> With something like this, should I be looking to create classes to hold this type of functionality?

I usually like whichever solution requires the least amount of source code.
Christopher Wells Send private email
Wednesday, May 24, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz