How can I reset a Property to it's default value?

S

steve bull

if I have a property something like :

[DefaultValue(0)]
public int Width
{
get {return m_width}
set {m_width = value}
}


As a calling program/routine how can I set m_width back to it's default value 0 without knowing that it is 0

i.e. something like this.Width = Width.DefaultValue rather than this.Width = 0


Thanks,

Steve
 
B

Bob Powell [MVP]

The DefaultValue attribute is there for the benefit of the PropertyGrid as
used in design time mode. The grid reads the attribute using reflection to
obtain the default value and provides the user with an option to "reset" the
value.

You could use a similar scheme to read the attributes of any given property
and see if it had a default value attribute. Then, if it does, force the
property to the value.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
G

Guest

DefaultValue does not set the value of your property.
It is used for the property-sheet. So you can click the Reset button Then
the default values are set.

Try to set the DefaultValue other than 0. And you'll see.
 
G

Guest

I got the following code from MSDN:

// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties
(this)"MyProperty"].Attributes;

/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());

You can use the myAttribute.Value to set the your property to the default
value.
 
S

steve bull

thanks, I will give it a try

Steve


I got the following code from MSDN:

// Gets the attributes for the property.
AttributeCollection attributes = TypeDescriptor.GetProperties
(this)"MyProperty"].Attributes;

/* Prints the default value by retrieving the DefaultValueAttribute
* from the AttributeCollection. */
DefaultValueAttribute myAttribute =
(DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];
Console.WriteLine("The default value is: " + myAttribute.Value.ToString());

You can use the myAttribute.Value to set the your property to the default
value.


steve bull said:
if I have a property something like :

[DefaultValue(0)]
public int Width
{
get {return m_width}
set {m_width = value}
}


As a calling program/routine how can I set m_width back to it's default value 0 without knowing that it is 0

i.e. something like this.Width = Width.DefaultValue rather than this.Width = 0


Thanks,

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top