0

Trying to set default values at runtime, the following class is used for my propertygrid:

public class zPosition
{
    public int _x;
    public int _y;

    public zPosition(int x, int y, int dx = 0, int dy = 0)
    {
        this._x = 10;
        this._y = 10;
        // set the default values here
    }

    [DisplayName("X"), DefaultValueAttribute(0)]
    public int X
    {
        get { return _x; }
        set { _x = value; }
    }

    [DisplayName("Y"), DefaultValueAttribute(0)]
    public int Y
    {
        get { return _y; }
        set { _y = value; }
    }

}

How would I set those default values in the class constructor?

Thanks

Joel
  • 385
  • 1
  • 4
  • 13
  • 2
    DefaultValue is only for the designer. It does not *set* any values. When the property value matches the DefaultValue attribute value, the designer will not save the information in the Designer file. – LarsTech Aug 07 '14 at 18:36
  • is there a work around to set the default value of a propertygrid property? I need to set them at runtime instead of using the following: DefaultValueAttribute(0) – Joel Aug 07 '14 at 18:54
  • How would you use it at runtime? – LarsTech Aug 07 '14 at 19:05
  • You can't do it with the attribute, write private `bool ShouldSerializeXxxx()` and `void ResetXxxx()` methods instead where Xxxx matches the property name. – Hans Passant Aug 07 '14 at 19:22
  • http://stackoverflow.com/questions/19749678/how-to-set-defaultvalueattribute-of-the-propertygrid-dynamically-or-at-runtime – mrbm Dec 25 '15 at 22:38

1 Answers1

0

The answer to your question is: DefaultValueAttribute cannot be set at runtime. Your understanding of attributes is just wrong.

However, depending on what you really want, there are other possible solutions. See comments and links.

maf-soft
  • 2,335
  • 3
  • 26
  • 49