tags:

views:

72

answers:

3

I have a function

protected void bindCurrencies(DropDownList drp)
    {
        drp.DataSource = dtCurrencies;
        drp.DataTextField = "CurrencyName";
        drp.DataValueField = "CurrencyID";
        drp.DataBind();
        drp.Items.Insert(0, new ListItem("Please Select"));
    }

I am binding a dropdown list using this. But sometimes I need to bind a ListBox also. I dont want to write a different function for listbox. How should I do this. I think Generics method is to be used here. But I dont have any idea about generics.

+9  A: 

Both DropDownList and ListBox inherit from ListControl, so if you change your function to take a ListControl as parameter, it should work well with both types:

protected void bindCurrencies(ListControl drp)
{
    drp.DataSource = dtCurrencies;
    // and so on...
Fredrik Mörk
+1  A: 

Ask for a ListControl instead. Both ListBox and DropDownList inherit it, so you can use it to contain both.

Generics is (in most cases) used if you need a class of something. For example, a list of strings would be a List in generics.

Jouke van der Maas
A: 

Pass the parameter as an Object, then simply use the object's:

.Getype()

around a select statement and cast it to that type.

Unlike the other solutions of Passing a ListControl, this will allow you to pass other controls types (and expand the logic as required).

EDIT 1 (in response to Sebastian):

 private void bindCurrencies(object PControl)
 {
    switch (PControl.GetType.ToString) {
        case "Windows.Forms.ListBox":
            Windows.Forms.ListBox ctrl = (Windows.Forms.ListBox)PControl;
            break;
        //Do Logic'
        case "Windows.Forms.DropDownList":
            break;
            //etc 
            //etc
    }
  }

always remember: KISS (Keep It Simple Stupid)

Darknight
Sebastian P.R. Gingter
I disagree, I'd rather maintain code that has a simple select case around the object type. Adding/Removing is simple as editing the select logic (not really difficult). As opposed to having multiple overloaded functions/methods dotted all over the place, which in my view is less maintainable. Using a simple select case would NOT break existing code any more than the overloaded version.
Darknight
Please read 'Clean Code' from Robert Martin. He describes very good why select / switch statements are not good for maintainable code and should be avoided.
Sebastian P.R. Gingter
So a caller of the function can pass a String, HTTPWebRequest or even an Employee object to the function, as all subclass Object. What would the behaviour of the function be then?
JBRWilkinson
Erm Case Else anyone? its no different to overloading, because behind at the root ITS doing Effectively the same thing, if X = something do Y.Theory and practice are two different things. Still show me explicitly how the above would be any more fundermentally different to using overloading methods/functions.Rather than downvoting without actually showing the evidence for it.
Darknight
Still not convinced. By arguments provided.
Darknight
@JBRWilkinson, if it doesn't match then nothing if the code does not default with anything (e.g case else) how again is this radically different to having overloaded methods or functions?
Darknight