Results 1 to 6 of 6

Thread: override in inherited control to disable property

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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.
    Last edited by wey97; Dec 29th, 2009 at 11:33 AM.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: override in inherited control to disable property

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    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?

  5. #5
    Member
    Join Date
    Dec 2009
    Posts
    34

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: override in inherited control to disable property

    Quote Originally Posted by cdaq View Post
    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;
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width