DefaultValueAttribute Not Working

  • Thread starter Thread starter Homa
  • Start date Start date
H

Homa

I've create a custom DataGridColumnStyle and when I try to assign a
DefaultValue to a property, it doesn't seems to have any effect.

Here is the code:

[DefaultValue(10)]
public int Test
{
get{return 0;}
set{}
}

the default value shown is 0 instead.
I also tried [DefaultValue(typeof(int), "10")] with no effect.

Thanks,
Homa Wong
 
That is just a test code. The actual code like this

[DefaultValue(100)]
public int MaxValue
{
get{return Convert.ToInt32(txtValue.Text);}
}

With this, the designer report error because txtValue.Text is default
to "".
To the Convert.ToInt32() fails.

It works only if I set the txtValue.Text to "100" in the constructor.

Also, even if I do a test code like this,
#region Property TestVal
#region Comments
/// <summary>
/// Private -
/// </summary>
#endregion
private int _TestVal = 0;
#region Comments
/// <summary>
/// Gets or Sets (Default = 0)
/// </summary>
#endregion
[DefaultValue(100)]
public int TestVal
{
get{return _TestVal;}
set{_TestVal = value;}
}
#endregion

it still returns 0 in the Designer instead of 100.

Homa

Brendan said:
Well, your Getter is always returning 0, isn't it?

Homa said:
I've create a custom DataGridColumnStyle and when I try to assign a
DefaultValue to a property, it doesn't seems to have any effect.

Here is the code:

[DefaultValue(10)]
public int Test
{
get{return 0;}
set{}
}

the default value shown is 0 instead.
I also tried [DefaultValue(typeof(int), "10")] with no effect.

Thanks,
Homa Wong
 
Back
Top