C# windows 8 apps. Shrinking code

I have here a bunch of codes that is likely similar but only differs in some static( the names).

public void Windows() {
        var win = new ListViewItem { Content = "Windows" };
        Main.Items.Add(win);
        win.Tapped += Windowstapp;
        string[] lv= new string[20];
        for (int x = 0; x <=19; x++) {
            lv[x] = "    Windows" + (x+1);

        }
    foreach (string val in lv) {
        var lvi = new ListViewItem { Content = val };
        Main.Items.Add(lvi);
        lvi.Visibility = Visibility.Collapsed;
        if (iden == false) {
            lvi.Visibility = Visibility.Visible;
            sv.ScrollToVerticalOffset(0);
        }

    }
    }
    public void Doors() {
        var door = new ListViewItem { Content = "Door" };
        Main.Items.Add(door);
        door.Tapped += DoorTapp;
        string[] lv = new string[20];
        for (int x = 0; x <= 19; x++)
        {
            lv[x] = "    Doors" + (x + 1);

        }
         foreach (string val in lv)
         {
             var lvi = new ListViewItem { Content = val };
             Main.Items.Add(lvi);
             lvi.Visibility = Visibility.Collapsed;
             if (id == false)
             {
                 lvi.Visibility = Visibility.Visible;
                 sv.ScrollToVerticalOffset(0);
             }
         }

    }

I'm planning to create a method like this:

 public void House(string a) {
        var win = new ListViewItem { Content = a };
        Main.Items.Add(win);
        win.Tapped += Windowstapp;
        string[] lv= new string[20];
        for (int x = 0; x <=19; x++) {
            lv[x] = "    " + a + (x+1);

        }
    foreach (string val in lv) {
        var lvi = new ListViewItem { Content = val };
        Main.Items.Add(lvi);
        lvi.Visibility = Visibility.Collapsed;
        if (iden == false) {
            lvi.Visibility = Visibility.Visible;
            sv.ScrollToVerticalOffset(0);
        }

    }
    }

But I got a problem in win. Tapped += WindowsTapp; because they don't share same event, What should I do to make this possible? Maybe like win.Tapped += a+WindowsTapp?

Thanks in Advance!

Jon Skeet
people
quotationmark

Just pass in the delegate you want to use to subscribe to the event, as well:

// I can't find the docs for ListViewItem.Tapped right now - adjust the
// type of the "handler" parameter to match the event
public void CreateItem(string contentName, TappedEventHandler handler)
{
    var item = new ListViewItem { Content = a };
    Main.Items.Add(item);
    item.Tapped += handler;
    ...
}

Then call it with:

CreateItem("Door", DoorTapped);
CreateItem("Window", WindowTapped);

(I've change the method names to be more conventional - you'd need to change the declarations as well, of course.)

people

See more on this question at Stackoverflow