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 by casting to the IExtentedControl
interface which 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);
}
}
I don't think you need to implement the IExtendedControl
interface in the ExtendedControlHelper
class. Moreover I corrected the grammar of the interface name from IExtendendControl
to IExtendedControl
.
EDIT :
Another solution could be to use the dynamic
keyword. The dynamic keyword is like object
but the type is checked at run time instead of compile time:
public static class ExtendedControlHelper
{
public static void AddRequiredFieldValidator(this dynamic control)
{
//...
}
public static void ConfigureAndAddValidator(this dynamic control, BaseValidator validator)
{
//...
}
}
The dynamic type doesn't exist at run time because it's solved by the compiler, so if you write this: myExtendedTextBox.AddRequiredFieldValidator();
the method is solved as
//...
public static void AddRequiredFieldValidator(this ExtendedTextBox control)
{
//...
}
and so on. So the type can change continuously at run time.
For more informations give a look here.
This calls for a straightforward binary search - only instead of returning failure when the bounds converge without a hit, you return the next lowest index. (Unless the total number of values is so small that it easily fits into memory - then a complete cache is the way to go.)
Best Answer
As you already mentioned for newer C# version you can wrap
Guid
value with therecord
type. You will get required equality functionality for "free".For older C# versions you can wrap
Guid
with the dedicated struct type, but then you need to implement equality by yourself.For multiple types you can accept possible "duplication", it would be "safe duplication", because implementation will not change.
You can introduce some helper extension methods