So I have made a bunch of user controls in my project which integrate into a system automatically that tracks certain things about them. But I’m unsure on how to do a certain part without it looking really ugly.
All the Controls are extended from different Control types (Panel, Textbox, Combobox, etc) but have several of the exact same methods.
What I would like to do is avoid this:
public void SendMyMessage(Control thisControl)
{
if(thisControl is myPanel) (thisControl as myPanel).SendMessage();
else if(thisControl is myComboBox) (thisControl as myComboBox).SendMessage();
else if(thisControl is myTextbox) (thisControl as myTextbox).SendMessage();
else if(thisControl is myLabel) (thisControl as myLabel).SendMessage();
}
And would rather have a more simple method that would let me call that in 1 line. (Noting that the SendMessage() function I am calling does different things on different controls, but requires no arguments and is called the same way)
I would suggest to implement something like
IMessageSenderinterface in all your controls. So you can have only one check:where interface looks like this: