Reputation: 1211
I don't know if this is possible but this is what I was thinking:
public class ValidationControl<T> where T : Control, new()
{
[Browsable(true)]
[Category("Validation")]
[DefaultValue(false)]
public bool Required { get; set; }
public ValidationControl() { Required = false; }
public virtual void RunValidation() { ... }
}
Then for all of my custom controls I could simply use the generic control as a reusable base class:
public class ValidationTextBox : ValidationControl<TextBox> { }
public class ValidationComboBox : ValidationControl<ComboBox> { }
I understand that I could use interfaces but then I would have to retype/copy & paste the required property, etc., for each new control I make. Also I am unable to override any virtual properties / methods this way for TextBox/ComboBox. Is this possible?
Upvotes: 3
Views: 567
Reputation: 22945
Inheriting is something different then using generics. You want to specify in a 'generics' way what your class needs to inherit from, and no, that is not going to work. You already said it, you cannot override any properties, which is because you do not inherit from your T.
For your control to work you must inherit from your control type (T).
Upvotes: 1
Reputation: 3407
Also I am unable to override any virtual properties / methods this way for TextBox/ComboBox. Is this possible?
You won't be able to override anything from TextBox/ComboBox. You can only override virtual methods from base class, in Your exemple, only overridable method is RunValidation() (ValidationControl being base class).
You class can contain TextBox/ComboBox, f. e.:
private T Control;
and make some calls to it but that's all.
Upvotes: 0