Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m building some custom web controls in .NET using C#. The controls inherit fr

ID: 642121 • Letter: I

Question

I'm building some custom web controls in .NET using C#. The controls inherit from the standard web controls, and add additional properties and functionality (e.g. I'm creating an 'extendedTextBox' and I'm adding a 'required' property, which if set to 'true' will add a .NET required field validator to the control automatically. I'm doing this for a number of web controls (e.g. radioButtonList, textArea). They share some common properties and methods, for example I have an AddRequiredFieldValidator method that uses some of the extended properties I've added. I'd like to share the common properties and methods. I've tried adding the methods to a separate class as extension methods for a web control. To achieve this I've implemented an interface that defines the additional shared properties, and am using it like this: public interface IExtendendControl { string RequiredMessage { get; set; } string ValidatorCssClass { get; set; } bool ClientScript { get; set; } RequiredFieldValidator rfv { get; set; } } public static class ExtendedControlExtensions : IExtendendControl { public static void AddRequiredFieldValidator(this IExtendendControl control) { control.rfv = new RequiredFieldValidator(); control.rfv.ErrorMessage = control.RequiredMessage; ConfigureAndAddValidator(control, control.rfv); } public static void ConfigureAndAddValidator(this IExtendendControl control, BaseValidator validator) { validator.ControlToValidate = control.ID; validator.Display = ValidatorDisplay.Dynamic; validator.CssClass = "validationMessage "; validator.CssClass += control.ValidatorCssClass; validator.EnableClientScript = control.ClientScript; control.Controls.Add(validator); } } Trouble is that the 'ConfigureAndAddValidator' method now doesn't know anything about the 'ID' or 'ClientScript' properties of the control since 'IExtendedControl' only defines the custom properties, not the standard properties of a web control. So I tried adding a base class that inherits from WebControl and implements the interface, like this: public interface IExtendendControl { string RequiredMessage { get; set; } string ValidatorCssClass { get; set; } bool ClientScript { get; set; } RequiredFieldValidator rfv { get; set; } } public class BaseExtendedControl : WebControl, IExtendendControl { public string RequiredMessage { get; set; } public string ValidatorCssClass { get; set; } public bool ClientScript { get { return ClientScript = true; } set { } } public RequiredFieldValidator rfv { get { return rfv = new RequiredFieldValidator(); } set { } } } public static class ExtendedControlHelper : IExtendendControl { public static void AddRequiredFieldValidator(this BaseExtendedControl control) { BaseExtendedControl extendedControl = (BaseExtendedControl)control; extendedControl.rfv = new RequiredFieldValidator(); extendedControl.rfv.ErrorMessage = extendedControl.RequiredMessage; ConfigureAndAddValidator(control, extendedControl.rfv); } public static void ConfigureAndAddValidator(this BaseExtendedControl control, BaseValidator validator) { validator.ControlToValidate = control.ID; validator.Display = ValidatorDisplay.Dynamic; validator.CssClass = "validationMessage "; validator.CssClass += control.ValidatorCssClass; validator.EnableClientScript = control.ClientScript; control.Controls.Add(validator); } } The trouble now is that in my extendedTextBox class I can't cast my extendedTextBox as a 'BaseExtendedControl' to use the extension methods, as the extendedTextBox inherits from the standard TextBox class like this: public class ExtendedTextBox : TextBox, IExtendendControl so there's no common base to cast ExtendedTextBox as 'BaseExtendedControl' as it doesn't inherit from it. I also can't just pass the extended web control objects as a parameter into the shared methods as the extended controls are of different types (e.g. they inherit from TextBox, RadioButtonList and so on). If I specify the expected type being passed into 'standard' methods as 'WebControl' it doesn't work as the extended controls have the additional properties. As I can't use true multiple inheritance in C#, how would I design this to be able to share the methods and properties?

Explanation / Answer

As I can't use true multiple inheritance in C#, how would I design this to be able to share the methods and properties?

This is the right way to share the methods and properties. But:

The trouble now is that in my extendedTextBox class I can't cast my extendedTextBox as a 'BaseExtendedControl' to use the extension methods, as the extendedTextBox inherits from the standard TextBox class like this:

public class ExtendedTextBox : TextBox, IExtendedControl
so there's no common base to cast ExtendedTextBox as 'BaseExtendedControl' as it doesn't inherit from it.

Yes, you're right there is no common class, but you can solve the problem casting to the IExtentedControl interface that is common to your classes and contains the methods that you need. So what about:

public static class ExtendedControlHelper
{
public static void AddRequiredFieldValidator(this IExtendedControl control)
{
control.rfv = new RequiredFieldValidator();
control.rfv.ErrorMessage = control.RequiredMessage;
ConfigureAndAddValidator(control, control.rfv);
}

public static void ConfigureAndAddValidator(this IExtendedControl control, BaseValidator validator)
{
validator.ControlToValidate = control.ID;
validator.Display = ValidatorDisplay.Dynamic;
validator.CssClass = "validationMessage ";
validator.CssClass += control.ValidatorCssClass;
validator.EnableClientScript = control.ClientScript;
control.Controls.Add(validator);
}
}
PS : I think you don't need to implement the IExtendedControl interface in the ExtendedControlHelper class and another thing, I corrected the grammar of the interface from IExtendendControl to IExtendedControl.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote