473,725 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What does this mean [DefaultValue(fa lse)]

Hello!!

I have some demo programs written in C# and they have this construction
"[DefaultValue(fa lse)]" see below.
I haven't seen this before so what does it mean ?

[DefaultValue(fa lse)]
public bool ShowDropDownBut tons
{
get { return showDropDownBut tons; }
set
{
if (showDropDownBu ttons != value)
{
showDropDownBut tons = value;
base.OnChanged( InvalidationMod e.ColumnWithout Header, false);
}
}
}//end property ShowDropDownBut tons

//Tony
Apr 25 '06 #1
10 3312
It simply tells the system what the default value is *for things that
respect DefaultValue*; typically used by the designer (for controls) to
avoid putting in unnecessary lines into InitializeCompo nent, but this is
also used in WSDL (it makes the property optional with a default) and a few
other places.

Note that this is not /quite/ the same as having a default (ctor etc) value
against the underlying field, as external components don't have (legitimate)
access to the field. It really just formalises it for use by external code;
note that it would be quite unusual to have a DefaultValue that didn't match
the underlying fields defaulted value...

Marc
Apr 25 '06 #2
I agree with what Marc wrote, just making some additional comments:

the DefaultValue attribute is just markup to tell external code what the
default value of a property is supposed to be -- it does not actually affect
the initial value of the variable.

Why this is interesting:
It allows for some optimizations. For example, in the code that you posted,
if you drop the control on a form, and leave ShowDropDownBut tons set to
false, then the Visual Studio form designer will not add initialization code
to set ShowDropDowns to false (because it understands that it will start
out false anyway). That means that there is one less line of code to jit
and execute when a form is initializing.

If you have a form with a lot of controls, and all of the controls include
DefaultValue attributes, you can cut down on the load time of your forms a
bit.
"tony" <jo************ *****@telia.com > wrote in message
news:ue******** *****@TK2MSFTNG P03.phx.gbl...
Hello!!

I have some demo programs written in C# and they have this construction
"[DefaultValue(fa lse)]" see below.
I haven't seen this before so what does it mean ?

[DefaultValue(fa lse)]
public bool ShowDropDownBut tons
{
get { return showDropDownBut tons; }
set
{
if (showDropDownBu ttons != value)
{
showDropDownBut tons = value;
base.OnChanged( InvalidationMod e.ColumnWithout Header,
false);
}
}
}//end property ShowDropDownBut tons

//Tony

Apr 25 '06 #3
Hello!!

Here I have the class called DynamicallyColu mn. What will be the difference
if I remove this statement [DefaultValue(fa lse)]?
I have made some tests this property showDropDownBut tons is false even if
you have this statement [DefaultValue(tr ue)] or
remove the initialization on showDropDownBut tons to false.
So this has no affect on the initialization.

So I still don't understand what it's purpose is. You mentioned something
about optimization. Can you explain that?

public class DynamicallyColu mn : DynamicallyAuto DetectDataTypeC olumn
{
private bool showDropDownBut tons = false;
public DynamicallyColu mn(string titel ) : base(titel)
{
this.showDropDo wnButtons = showDropDownBut tons;
}

[DefaultValue(fa lse)]
public bool ShowDropDownBut tons
{
get { return showDropDownBut tons; }
set
{
if (showDropDownBu ttons != value)
{
showDropDownBut tons = value;
base.OnChanged( InvalidationMod e.ColumnSummary ,
false);
}
}
}
}

//Tony
"J.Marsch" <jm*****@newsgr oup.nospam> skrev i meddelandet
news:Oq******** ******@TK2MSFTN GP05.phx.gbl...
I agree with what Marc wrote, just making some additional comments:

the DefaultValue attribute is just markup to tell external code what the
default value of a property is supposed to be -- it does not actually affect the initial value of the variable.

Why this is interesting:
It allows for some optimizations. For example, in the code that you posted, if you drop the control on a form, and leave ShowDropDownBut tons set to
false, then the Visual Studio form designer will not add initialization code to set ShowDropDowns to false (because it understands that it will start
out false anyway). That means that there is one less line of code to jit
and execute when a form is initializing.

If you have a form with a lot of controls, and all of the controls include
DefaultValue attributes, you can cut down on the load time of your forms a
bit.
"tony" <jo************ *****@telia.com > wrote in message
news:ue******** *****@TK2MSFTNG P03.phx.gbl...
Hello!!

I have some demo programs written in C# and they have this construction
"[DefaultValue(fa lse)]" see below.
I haven't seen this before so what does it mean ?

[DefaultValue(fa lse)]
public bool ShowDropDownBut tons
{
get { return showDropDownBut tons; }
set
{
if (showDropDownBu ttons != value)
{
showDropDownBut tons = value;
base.OnChanged( InvalidationMod e.ColumnWithout Header,
false);
}
}
}//end property ShowDropDownBut tons

//Tony


Apr 26 '06 #4
It only affects *things that use this control* (via the IDE).

IIRC, it goes like this:
* You drag your shiny new control onto a form
* It runs the default ctor, which defaults (via the field default) to false.
***THIS IS THE IMPORTANT ONE**
* Now, when the designed attempts to write that to InitialiseCompo nent, it
queries DefaultValue; if it exists, it compares the values; if the
DefaultValue is different to the current property value, then an entry gets
written to the file.

Equally, I suspect this controls the bold / non-bold highlighting in the
designer.

Ironically and confusingly, what it does *NOT* do (in this case) is specify
the actual default value; this is defined by the field. The problem here is
that you have a different ctor value to DefaultValue; this is a recipe for
pain.

This is all necessary because it can't determine whether property "x" is
(for instance) date-driven (think .Now) {could be equally non-determinate in
any number of ways}.

Does that help?

Marc
Apr 26 '06 #5
Hello!!

Only some questions on your post.
You say "You drag your shiny new control onto a form" it's ok no strange
about that
You say It runs the default ctor, which defaults (via the field default) to
false. It's ok no strange about that either.

But what do you mean here
Now, when the designed attempts to write that to InitialiseCompo nent, it
queries DefaultValue; if it exists, it compares the values; if the
DefaultValue is different to the current property value, then an entry gets written to the file.
Can you make a very simple example about what you mean using this
DefaultValue?

//Tony

"Marc Gravell" <ma**********@g mail.com> skrev i meddelandet
news:#e******** *****@TK2MSFTNG P05.phx.gbl... It only affects *things that use this control* (via the IDE).

IIRC, it goes like this:
* You drag your shiny new control onto a form
* It runs the default ctor, which defaults (via the field default) to false. ***THIS IS THE IMPORTANT ONE**
* Now, when the designed attempts to write that to InitialiseCompo nent, it
queries DefaultValue; if it exists, it compares the values; if the
DefaultValue is different to the current property value, then an entry gets written to the file.

Equally, I suspect this controls the bold / non-bold highlighting in the
designer.

Ironically and confusingly, what it does *NOT* do (in this case) is specify the actual default value; this is defined by the field. The problem here is that you have a different ctor value to DefaultValue; this is a recipe for
pain.

This is all necessary because it can't determine whether property "x" is
(for instance) date-driven (think .Now) {could be equally non-determinate in any number of ways}.

Does that help?

Marc

Apr 26 '06 #6
When all else fails, an example:

Code is below, but it consists of a custom control with 6 properties; there
are alternating true/false, but: the first two have no DefaultValue, the
second two have DefaultValue(tr ue), and the last two have
DefaultValue(fa lse).

Compile it, and drag it onto a form, and I get the following displayed in
the IDE (where * denotes bold - i.e. customised value):

Prop1 : *True*
Prop2 : *False*
Prop3 : True
Prop4 : *False*
Prop5 : *True*
Prop6 : False

This alternating patterm tells me immediately that the ctor has won over the
DefaultValue; so DefaultValue is not used when /creating/ objects (even via
the IDE). The relevent generated code is:

this.defaultedC ontrol1.Prop1 = true;
this.defaultedC ontrol1.Prop2 = false;
this.defaultedC ontrol1.Prop4 = false;
this.defaultedC ontrol1.Prop5 = true;

This is identical to the bold text as previous. Note that where the current
value on the control instance matches the value in DefaultValue (i.e. cases
3 and 6), then no code is generated. This also supports the "optimisati on"
claim by another poster - less lines to trawl through.

Does that explain what I am saying? The moral is: if there is a sensible
(fixed) default for a property, then use DefaultValue; if a property isn't
predictable (i.e. it is itself an aggregate of 3 other fields, or depends on
the time, whatever), then you can't define a DefaultValue.

Note also that there are other attributes that control what appears in the
initialisation code - for instance DesignerSeriali zationVisibilit y (IIRC).

Marc

=============== ====

using System;
using System.Windows. Forms;
using System.Componen tModel;

namespace Test {
class DefaultedContro l : Control {
private bool _field1 = true, _field2 = false,
_field3 = true, _field4 = false,
_field5 = true, _field6 = false;

// no default; ctor true
public bool Prop1 {
get { return _field1; }
set { _field1 = value; }
}
// no default; ctor false
public bool Prop2 {
get { return _field2; }
set { _field2 = value; }
}
[DefaultValue(tr ue)] // and ctor true
public bool Prop3 {
get { return _field3; }
set { _field3 = value; }
}
[DefaultValue(tr ue)] // and ctor false
public bool Prop4 {
get { return _field4; }
set { _field4 = value; }
}
[DefaultValue(fa lse)] // and ctor true
public bool Prop5 {
get { return _field5; }
set { _field5 = value; }
}
[DefaultValue(fa lse)] // and ctor false
public bool Prop6 {
get { return _field6; }
set { _field6 = value; }
}
}
}
Apr 26 '06 #7
oh - and the other moral: the following is daft (the comment is correct,
though); don't do it... where DefaultValue is defined, it should ideally
match the ctor default value:

private int _field = 5;
[DefaultValue(17 )] // WTF? should probably be 5
public int SomeProperty {
get {return _field;}
set {_field = value;}
}

You don't have to stick with the native defaults (false, 0, null), but where
you don't, keep them in sync. This is why I always declare my underlying
fields right next to the properties, so I can spot any goofs like the above.

Marc
Apr 26 '06 #8
Better yet, tie them together using a private const.

private const int FieldDefault = 5;
private int _field = FieldDefault;
[DefaultValue(Fi eldDefault)]

"Marc Gravell" <ma**********@g mail.com> wrote in message
news:Oz******** ******@TK2MSFTN GP04.phx.gbl...
oh - and the other moral: the following is daft (the comment is correct,
though); don't do it... where DefaultValue is defined, it should ideally
match the ctor default value:

private int _field = 5;
[DefaultValue(17 )] // WTF? should probably be 5
public int SomeProperty {
get {return _field;}
set {_field = value;}
}

You don't have to stick with the native defaults (false, 0, null), but
where you don't, keep them in sync. This is why I always declare my
underlying fields right next to the properties, so I can spot any goofs
like the above.

Marc

Apr 26 '06 #9
Hello!

I'm trying to copy your code and compile it and load it into the toolbox but
I run into problem.
I trying to understand this about [DefaultValue(fa lse)]
This is what I do. You can correct me what I should do instead.

1. First I create a window application
2. I add a new project into the solution which is a Window Control Library.
3.I copy your code into the custom control file in the Window Control
Library'
4. I build this Window Control Library which is build into a dll
5. I add this Window Control Library dll into the toolbox.
6. I get the following error "There are no components in
....(WindowCont rolLibrary.dll that can
be placed on the toolbox.

When this failed I created a user control file in this Window Control
Library and copy your code
into the file and did the same build the Window Control Library into a dll
and tried to add to the toolbox
but got the same error message.
What should I do to test your code??
//Tony


"Marc Gravell" <ma**********@g mail.com> skrev i meddelandet
news:Oz******** ******@TK2MSFTN GP04.phx.gbl...
oh - and the other moral: the following is daft (the comment is correct,
though); don't do it... where DefaultValue is defined, it should ideally
match the ctor default value:

private int _field = 5;
[DefaultValue(17 )] // WTF? should probably be 5
public int SomeProperty {
get {return _field;}
set {_field = value;}
}

You don't have to stick with the native defaults (false, 0, null), but where you don't, keep them in sync. This is why I always declare my underlying
fields right next to the properties, so I can spot any goofs like the above.
Marc

Apr 27 '06 #10

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

Similar topics

10
1765
by: Sylvain Thenault | last post by:
Hi there ! Can someone explain me the following behaviour ? >>> l = >>> 0 in (l is False) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: iterable argument required >>> (0 in l) is False
3
6191
by: Doug Holton | last post by:
I've got a boolean class property that is almost always false. How can I specify that a class property should not be included in the xml serializer output if it is false? I'm using . will never print it if it is true either.
3
4253
by: Aamir Ghanchi | last post by:
How can I set the DefaultValue property of DataColumn to false in C# this.objdsMyDataSet.tblMyTable.MyBooleanColumn.DefaultValue = false; I have also tried setting it to System.Data.SqlTypes.SqlBoolean.False; but in either case I get ArgumentException. What is wrong, it looks straightforward to me.
1
1704
by: Sanjay Pais | last post by:
I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity to the html in the page. However, when I cycle through the controls on the page using this code, I cant seem to be able to access the Attribute collection. However, if I were to add the tag to a regular TextBox, the Attribute is available. My recursive function looks like this:...
7
8894
by: Oleg Subachev | last post by:
What is C#'s analogue of Delphi's TAction ? If there is no one, how to implement the same functionality > Oleg Subachev
1
4229
by: matthewtech | last post by:
In Visual Studio 2005, using Visual Basic, Is there a way to set the autosize property to false as a standard, and change it to true as needed? Or does it always start out as true when a label is brought in to a form? This is probably a very basic question, but I appreciate any help anyone can offer.
4
5356
by: SteveT | last post by:
I have a boolean property that I want to serialize to disk using XmlSerializer. I noticed that if the property includes the attribute "DefaultValue(true)" or "DefaultValue(false)" that the property will not serialilze to an XML file. If the attribute is removed the serialization occurs just fine. Is there a way around this bug? This behavior seems to work with any type of property. --
0
1934
by: pargat.singh | last post by:
Hi Everyone: I have a gridview control with AllowSorting="True" and work ok. But my page will be used by different user from different region so i need to change the HeaderText based on language like english or french etc.When i updat ethe header on GridView1_RowCreated sorting does not work anymore. Below are my code. Is this because of BoundField. Can someone please help me with this issue. Thanks in advance. <asp:GridView...
10
1291
by: shapper | last post by:
Hello, I have a custom control under namespace MyNameSpace.WebControls with a property of type validation: ' Validation Private _Validation As Validation < _ Bindable(True), _ Category("Behavior"), _
0
8889
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
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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
9116
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
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3228
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
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.