473,604 Members | 2,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve
Nov 17 '05 #1
7 7397
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.

"steve bull" <bu****@comcast .net> wrote in message
news:w3******** *************** ***@4ax.com...

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.DefaultVa lue rather than this.Width
= 0
Thanks,

Steve

Nov 17 '05 #2
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.

"steve bull" wrote:

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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve

Nov 17 '05 #3
I got the following code from MSDN:

// Gets the attributes for the property.
AttributeCollec tion attributes = TypeDescriptor. GetProperties
(this)"MyProper ty"].Attributes;

/* Prints the default value by retrieving the DefaultValueAtt ribute
* from the AttributeCollec tion. */
DefaultValueAtt ribute myAttribute =
(DefaultValueAt tribute)attribu tes[typeof(DefaultV alueAttribute)];
Console.WriteLi ne("The default value is: " + myAttribute.Val ue.ToString());

You can use the myAttribute.Val ue to set the your property to the default
value.
"steve bull" wrote:

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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve

Nov 17 '05 #4
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.

"steve bull" <bu****@comcast .net> wrote in message
news:w3******** *************** ***@4ax.com...

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.DefaultVa lue rather than this.Width
= 0
Thanks,

Steve

Nov 17 '05 #5
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.

"steve bull" wrote:

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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve

Nov 17 '05 #6
I got the following code from MSDN:

// Gets the attributes for the property.
AttributeCollec tion attributes = TypeDescriptor. GetProperties
(this)"MyProper ty"].Attributes;

/* Prints the default value by retrieving the DefaultValueAtt ribute
* from the AttributeCollec tion. */
DefaultValueAtt ribute myAttribute =
(DefaultValueAt tribute)attribu tes[typeof(DefaultV alueAttribute)];
Console.WriteLi ne("The default value is: " + myAttribute.Val ue.ToString());

You can use the myAttribute.Val ue to set the your property to the default
value.
"steve bull" wrote:

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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve

Nov 17 '05 #7

thanks, I will give it a try

Steve
On Wed, 29 Jun 2005 23:37:02 -0700, Marinus Holkema <Ma************ @discussions.mi crosoft.com> wrote:
I got the following code from MSDN:

// Gets the attributes for the property.
AttributeCollec tion attributes = TypeDescriptor. GetProperties
(this)"MyProper ty"].Attributes;

/* Prints the default value by retrieving the DefaultValueAtt ribute
* from the AttributeCollec tion. */
DefaultValueAtt ribute myAttribute =
(DefaultValueAt tribute)attribu tes[typeof(DefaultV alueAttribute)];
Console.WriteLi ne("The default value is: " + myAttribute.Val ue.ToString());

You can use the myAttribute.Val ue to set the your property to the default
value.
"steve bull" wrote:

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.DefaultVa lue rather than this.Width = 0
Thanks,

Steve


Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2175
by: Shabam | last post by:
I have a text field that's pre-filled with data. Suppose the user edits it, but decides he wants to reset the data back to the original pre-filled data, how can I do that?
2
8083
by: Matt | last post by:
i need to implement a clear button to clear all the fields in the form, but i am thinking i can just use reset button. <input type="reset" name="reset" value="CLEAR"> The first thought is that if there is default value reset and clear will become different. My understanding is that reset means to restore the original page before
8
9550
by: Ryan Stewart | last post by:
Is there a way to reset the style property of an HTML element to some default or to all empty values? I have a group of elements whose style settings may be changed arbitrarily at certain points in a script (maybe set the background color and font weight of one element and the text align of another), but at another point I want to undo all of these changes and go back to the original state (which is actually all empty styles). Is there a...
5
5947
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset): Public Class DatePicker2...
2
4832
by: Lance | last post by:
I want to be able to reset a complex property in a PropertyGrid. I know that for properties that are ValueTypes you can include System.ComponentModel.DefaultValue in the declaration of the property. But, for complex property types (e.g., instance types) this does not work because System.ComponentModel.DefaultValue requires a constant value In order to indicate if a property should be serialized you can include a boolean function named...
22
5598
by: Kd | last post by:
I have the following set up to give me aresponse number R05-001 I would like it to reset to R06-001 at new year ResponseNo: "R" & Right(Format(Date(),"yyyy"),2) & "-" & Format(,"000") This is an expression in a query any help is apprecieated Ken
2
2289
by: TD | last post by:
I've read several posts here that say global variables are reset whenever an unhandled error occurs. I want to use a custom form property instead of a global variable to store a boolean value. My question is does a custom form property get reset also? Was also wondering if there is any reason one would ever use a global variable since the consensus seems to be to never use global variables?
11
7407
by: newbie | last post by:
i have a form in which a hidden field (initial value as '0', and my javascript set it to '1' when an event is trigged). In the same form, i have a reset field. But I realized that the hidden field is not reset to '0' when i push the reset button. If I simply change the node from "<input type="hidden" id='IsChanged' value='0'>" to "<input type="text" id='IsChanged' value='0'>" Everything is working as expected (the value is reset to '0'...
3
1706
by: artev | last post by:
if in a page I have a search form and I want use the method reset() for reinsert the default values, I notice that after a research, for the method reset(), the default values aren't more that first time loaded in the form but that of the form set for the search (I think so); see that the form is inner the same result's page, pheraphs the reset() 'see' how default values only the last form and not those that is loaded initially;
0
7997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8409
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8280
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3907
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3955
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2434
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1526
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.