views:

150

answers:

1

I have a very basic application that has buttons in a toolstrip. When a button is clicked it adds the appropriate User Control to my List<Control>. I then loop through that list creating either TabPages or mdi forms, with the user controls as children. I created an interface for these user controls so that I can ensure that they contain specific variables... specifically, one for the text of its parent TabPage or mdi form.

When I pass my user controls to functions (with a Control signature), I cannot reference variables defined in the interface. I'm wondering what the best way to go about this is. Should I create a new class that inherits "Control" and implements my interface, then change my user controls to be an instance of this new type?

I'm using C#

+4  A: 

You'll need to cast the Control parameter to your method to your appropriate UserControl, and verify that it's the correct type. You would then be able to access your API:

public void ProcessMyUserControl(Control control) {
    MyUserControl myControl = control as MyUserControl;
    if (myControl == null) 
    {
         // This was a different type of control, not your user control...
         return;
    }
    myControl.MyMethod();
}

It might be simpler to save your List directly as List<MyUserControl> instead of List<Control>. Then just pass the methods your class/interface directly.

Reed Copsey