Limit base class parameter type

I have a code like this:

public static void ToUpperCase(params Control[] controls) { foreach (Control oControl in controls) { if (oControl is TextBox) { oControl.TextChanged += (sndr, evnt) => { TextBox txtControl = sndr as TextBox; int pos = txtControl.SelectionStart; txtControl.Text = txtControl.Text.ToUpper(); txtControl.SelectionStart = pos; }; } else if (oControl is ComboBox) { oControl.TextChanged += (sndr, evnt) => { ComboBox cmbControl = sndr as ComboBox; int pos = cmbControl.SelectionStart; cmbControl.Text = cmbControl.Text.ToUpper(); cmbControl.SelectionStart = pos; }; } else throw new NotImplementedException(oControl.GetType().DeclaringType.ToString() + " is not allowed."); } } 

I want to limit params Control[] controls to accept only TextBox and ComboBox types.

My code is in C #, framework 4, in VS2010Pro, the project is in WinForms.

Please, help. Thanks in advance.

+4
source share
3 answers

Usually you should use a common base class for TextBox or ComboBox, but this is already Control. Also you cannot change base classes.

The best I can think of is to add Debug.Assert for type checking. Sort of:

 foreach (var control in controls) Debug.Assert((control is TextBox) || (control is ComboBox)); 
+4
source

You cannot ... they do not have a good common ancestor.

What you can (and probably should) do is do two overloads of your method that take the parameters of each of them:

 public static void ToUpperCase(params TextBox[] controls) { foreach (TextBox oControl in controls) oControl.TextChanged += (sndr, evnt) => { TextBox txtControl = sndr as TextBox ; int pos = txtControl.SelectionStart; txtControl.Text = txtControl.Text.ToUpper(); txtControl.SelectionStart = pos; }; } public static void ToUpperCase(params ComboBox[] controls) { foreach (ComboBoxControl oControl in controls) oControl.TextChanged += (sndr, evnt) => { ComboBox txtControl = sndr as ComboBox; int pos = txtControl.SelectionStart; txtControl.Text = txtControl.Text.ToUpper(); txtControl.SelectionStart = pos; }; } 
+5
source

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 { //TextChanged event is already here, just use it. //Implement TransformValueToUpperCase in a way that suits your control public void TransformValueToUpperCase() { int pos = this.SelectionStart; this.Text = this.Text.ToUpper(); this.SelectionStart = pos; } } public class UpperCaseableComboBox : ComboBox, ISupportUpperCase { //TextChanged event is already here, just use it. //Implement TransformValueToUpperCase in a way that suits your control } 

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 } //constructor public UpperCaseableTextbox () { this.TextChanged += (sender, args) => { if (this.AlwaysInUpperCase) { int pos = this.SelectionStart; this.Text = this.Text.ToUpper(); this.SelectionStart = pos; } } } } 

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.

+4
source

Source: https://habr.com/ru/post/1395339/


All Articles