override in inherited control to disable property
I'm inheriting a control from TextBox and I want to disable the MaxLength property and fix it at 5. MaxLength is a virtual property of Control so I should override it.
Code:
public FixedTextBox() {
base.MaxLength = 5;
}
[Browsable(false)]
[DefaultValue(5)]
public override int MaxLength {
get {
return 5;
}
}
Now the MaxLength property should be read-only since I only have a 'get' modifier, right? If I assign to the MaxLength property of an instance of FixedTextBox there are no compile errors.
Code:
fixedTextBox1.MaxLength = 0;
Why does this not generate a compile error? Is it because the override modifier only overrides the 'get' modifier of Control.MaxLength but leaves the 'set' modifier alone?
Using the 'new' modifier fixes this issue and I understand why, I just don't understand why 'override' does not work.
Re: override in inherited control to disable property
Quote:
Why does this not generate a compile error? Is it because the override modifier only overrides the 'get' modifier of Control.MaxLength but leaves the 'set' modifier alone?
Yup, I believe that pretty much sums it up right there. You could try also overriding the set modifier and simply discard the new value passed in.
-tg
Re: override in inherited control to disable property
Makes sense but if I override the set modifier, since the property is public, then it can still be assigned to. I know it won't matter because it's discarded, I just don't want the appearance the property can be modified in an instance outside the class.
so create a private new MaxLength:
Code:
private new int MaxLength {
get {
return base.MaxLength;
}
set {
base.MaxLength = value;
}
}
Now if I drop a FixedTextBox on a Form, the MaxLength property appears in the Designer even though it's private. I can also assign to the property:
fixedTextBox1.MaxLength = 0;
The MaxLength appears to revert back to Control.MaxLength but inside the FixedTextBox class, the MaxLength refers to the new MaxLength property. So does this mean that you can have two different instances of the same property with the same name? One acting on the instance of the class and a different acting inside the class? Wow, I knew even less about C# OO than I thought.
Re: override in inherited control to disable property
But if the MaxLength is declared as public new int MaxLength with only the 'get' accessor then that totally replaces Control.MaxLength right? And essentially I can never totally get rid of the MaxLength property of an instance of FixedTextBox even though I only want the property visible inside the class?
Re: override in inherited control to disable property
Just get rid of new in the MaxLength property
Code:
public int MaxLength {
get {
return base.MaxLength;
}
}
This will hide the base classes implementation of MaxLength and will make your version read only.
Re: override in inherited control to disable property
Quote:
Originally Posted by
cdaq
Just get rid of new in the MaxLength property
Code:
public int MaxLength {
get {
return base.MaxLength;
}
}
This will hide the base classes implementation of MaxLength and will make your version read only.
I think 'new' would be preferred since it also hides the base implementation and prevents the warnings to use override or new.
Strangely enough, The ReadOnly attribute has to be set for the property in order for the designer not to attempt to assign a default value to the MaxLength which won't compile since there's only a 'get' accessor.
Code:
[ReadOnly(true)]
[Browsable(false)]
public new int MaxLength {
get {
return base.MaxLength;
}
}