Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DefaultValueAttribute

DefaultValueAttribute

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tylerl
    wrote on last edited by
    #1

    Is it possible to change the DefaultValue attribute for a property for a derived class without having to re-define the whole property?

    H 1 Reply Last reply
    0
    • T tylerl

      Is it possible to change the DefaultValue attribute for a property for a derived class without having to re-define the whole property?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      It actually depends on how you plan to use it. Normally, yes, you would have to override the property and attribute it with a different value for the DefaultValueAttribute constructor. If the object is only to be used with Component Model clients (like the PropertyGrid) you can implement ICustomTypeDescritor and when a PropertyDescriptor is requested for the property you can return a custom implementation that returns a different instance of DefaultValueAttribute from the PropertyDescriptor.Attributes. See the documentation for ICustomTypeDescriptor for more details. You can return the default implementation for most methods you don't need to override using the TypeDescriptor class (corresponding methods to ICustomTypeDescriptor take an additional parameter that basically says not to use any ICustomTypeDescriptor implementation). Since you care about the DefaultValueAttribute and this is generally used at design-time (though not necessary), you could also implement a custom designer that does essentially what I mentioned above. If this was a Component derivative you would implement a derivative of ComponentDesigner and override PostFilterAttributes. Generally speaking, though - it's far easier of a change and easier to maintain with less possibility of bugs to just override the property. It could be as easy as the following:

      public class MyBase
      {
      bool _boolProperty = true;
       
      [DefaultValue(true)]
      public virtual bool BoolProperty
      {
      get { return _boolProperty; }
      set { _boolProperty = value; }
      }
      }
       
      public class MyDerivative : MyBase
      {
      public MyDerivative()
      {
      BoolProperty = false;
      }
       
      [DefaultValue(false)]
      public override bool BoolProperty
      {
      get { return base.BoolProperty; }
      set { base.BoolProperty = value; }
      }
      }

      See, there's nothing really to implement - just set the member of the base class. Many classes within the .NET Framework base class library do this when necessary. It's just a cleaner design and doesn't depend on callers having to implement or use rather difficult interfaces. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups