I wish to declare a variable in such a way as it can be assigned only values which derive from Control and also implement the ISomething interface.
I intend to add the ISomething interface to derivatives of controls.
I would like to derive SpecialTextBox and SpecialDatePicker From TextBox and DatePicker and implement the ISomething interface on each.
I would like to be able to assign each of these controls to a variable whose type is ‘Control which also implements ISomething’ so that from there they could either have their ISomething methods invoked or could be added to a control collection on a form.
So…. How do I declare a variable of Type ‘Control which also implements ISomething’?
Ideally the answer to be in VB.Net but I would be interested in a C# method also.
One way to do this is with generics – i.e. a generic method:
Now you can call Foo only with other variables that are a
Controlthat implementsISomething– you don’t need to specify the generic, though:is all you need. If you’ve given it something that isn’t both a
ControlandISomething, the compiler will tell you.Update: I don’t ‘do’ VB, but reflector tells me that the above translates as: