OPTION ONE
If you want to be able to pass a mixed collection of text fields and combobox in your function and still have a static type check, then you can implement it as follows.
public interface ISupportUpperCase { event EventHandler ValueChanged; void TransformValueToUpperCase(); } public class UpperCaseableTextbox : Textbox, ISupportUpperCase {
Then your function will be:
public static void ToUpperCase(params ISupportUpperCase[] controls) { foreach (var oControl in controls) { oControl.TextChanged += (sndr, evnt) => { oControl.TransformValueToUpperCase(); } } }
By doing so, you get better encapsulation, since only a certain control needs to know HOW to make the ITS UpperCase value, and not some magic functions somewhere around. You can also easily enter additional controls that support this function without changing other functions.
OPTION TWO
In fact, you can completely get rid of this function using the same approach, just slightly change the interface to:
public interface ISupportUpperCase { bool AlwaysInUpperCase { get; set } }
so that your controls are fully responsible for this feature based on this flag:
public class UpperCaseableTextbox : Textbox, ISupportUpperCase { public bool AlwaysInUpperCase { get; set }
Therefore, instead of having a function, you can simply set the property when you need the control to always be uppercase and the control will control itself.
source share