"Tom Spink" <ts****@gmail.comskrev i meddelandet
news:eZ**************@TK2MSFTNGP02.phx.gbl...
Hi Peter,
Actually, it probably should. All you're doing by applying a DefaultValue
attribute to a property is, well, apply an attribute to a property. The
property *still* needs to be initialised with a value, and it gets
initialised to the value automatically that you've provided via the
DefaultValue attribute.
The attribute in no way causes the framework to start assigning values to
variable, without an *actual* assignment statement... that would be crazy
talk!
I'm not sure I understand what you mean here, but I think we are saying the
same thing :-)
I never stated that the DefaultValue attribute initalizes my variables!
This is what I do:
long m_lMaxVal = Int32.MaxValue;
public long MaxValue
{
get{ return m_lMaxVal;}
set{if( value >= MinValue ){m_lMaxVal = value;}}
}
and then I use any of this in the __ClassDiagram Custom Attribute dialog__
(I'm not setting the attributes in the code):
DefaultValue(Int32.MaxValue)
DefaultValue("Int32.MaxValue")
DefaultValue("long", "Int32.MaxValue")
DefaultValue("int", "Int32.MaxValue")
(the same thing applies for a simple property/variable of type bool)
bool m_bTest = false;
public bool ATest
{
get{return m_bTest;}
set{m_bTest=value;}
}
DefaultValue(true)
** THIS IS THE PROBLEM **
But the value is written to InitializeComponent anyway:
this.textBoxNumeric2.MaxValue = ((long)(2147483647));
This is an extract from msdn docs (the link given previously in this post):
"Code generators can use the default values also to determine whether code
should be generated for the member."
So atleast one of the ideas with the DefaultValue attribute is to prevent
code beeing generated in the InitalizeComponent function.
The confusing part here is what arguments the DefaultValue attribute takes,
as it seems it is able to take one or two arguments. This is not documented
as far as I know, or I have not been looking in the right place.