I have an Interface IDataControl:
public interface IDataControl
{
...
}
Then, I have a class with a variable of List<IDataControl>.
List<IDataControl> items;
The problem is when I try to add elements of this collection in a "Controls" property of another form or another control:
panel.Controls.Add(items[i]);
The IDE says me that "The best overloaded method matching......" because "IDataControl" is not inherited from "Control".
I understand the error, and I kwnow that I can do some casting like:
panel.Controls.Add( (Control)items[i] );
But, I want to know if there is a "cleaner" way to do this, without requiring a casting, having also strict type verifying.
So, is it possible to indicate that "IDataControl" only can be implemented by Control derived objects?
public interface IDataControl where this : Control
{
...
}
Or, is it possible to declare a variable of a class and a interface?
Something like this:
Control:IDataControl item;
List<Control:IDataControl> items;
Thanks