DefaultValue of Custom control

P

Peter Hartlén

Hi!

What's the correct syntax for the default value design time attribute, using
classdiagram view and Custom Attributes dialog.

For a boolean:
DefaultValue(true)
DefaultValue("true")
DefaultValue("bool","true")

For a max value of a property of type _long_ (although max value is set to
max of int32)
DefaultValue(Int32.MaxValue)
DefaultValue("Int32.MaxValue")
DefaultValue("long", "Int32.MaxValue")
DefaultValue("int", "Int32.MaxValue")

For a custom enum:
DefaultValue(TextBoxNumericType.WholeNumber)
DefaultValue("TextBoxNumericType.WholeNumber")
DefaultValue("TextBoxNumericType", "TextBoxNumericType.WholeNumber")

I'm not successfull in any of the cases above...

Thanks in advance!

/ Peter
 
P

Peter Hartlén

What I actually wonder is how I get the designer not to write the default
value into IntializeComponent, which is the main usage of DefaultValue
property.
For a max value of a property of type _long_ (although max value is set to
max of int32)
DefaultValue(Int32.MaxValue)
DefaultValue("Int32.MaxValue")
DefaultValue("long", "Int32.MaxValue")
DefaultValue("int", "Int32.MaxValue")

For this property I have the following declaration:
long m_lMaxVal = Int32.MaxValue;
public long MaxValue

{

get{ return m_lMaxVal;}

set

{

if( value >= MinValue )

{

m_lMaxVal = value;

}


}


}



But the value is written to InitializeComponent anyway:

this.textBoxNumeric2.MaxValue = ((long)(2147483647));
 
P

Peter Hartlén

Hi Chris, thanks for replying!

Yes, please read my second post, I do set the default value for the
underlying member in the class. So the _value_ of the property is set
correctly in the designer. BUT, if this value is the same as the
DefaultValue value, the designer shouldn't write it into the
InitializeComponent method, which always happens for me..

Any ideas on this?

/ Peter


"Chris Fulstow" <[email protected]> skrev i meddelandet
Should be one of these (for a bool):

VB <DefaultValue(False)>
C# [DefaultValue(false)]

Have you tired setting the attribute directly in the code rather than
using the dialog window?

More info here:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
 
T

Tom Spink

Peter said:
Hi Chris, thanks for replying!

Yes, please read my second post, I do set the default value for the
underlying member in the class. So the _value_ of the property is set
correctly in the designer. BUT, if this value is the same as the
DefaultValue value, the designer shouldn't write it into the
InitializeComponent method, which always happens for me..

Any ideas on this?

/ Peter


"Chris Fulstow" <[email protected]> skrev i meddelandet
Should be one of these (for a bool):

VB <DefaultValue(False)>
C# [DefaultValue(false)]

Have you tired setting the attribute directly in the code rather than
using the dialog window?

More info here:
http://msdn2.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute.aspx
Hi!

What's the correct syntax for the default value design time attribute,
using
classdiagram view and Custom Attributes dialog.

For a boolean:
DefaultValue(true)
DefaultValue("true")
DefaultValue("bool","true")

For a max value of a property of type _long_ (although max value is set
to max of int32)
DefaultValue(Int32.MaxValue)
DefaultValue("Int32.MaxValue")
DefaultValue("long", "Int32.MaxValue")
DefaultValue("int", "Int32.MaxValue")

For a custom enum:
DefaultValue(TextBoxNumericType.WholeNumber)
DefaultValue("TextBoxNumericType.WholeNumber")
DefaultValue("TextBoxNumericType", "TextBoxNumericType.WholeNumber")

I'm not successfull in any of the cases above...

Thanks in advance!

/ Peter

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!
 
P

Peter Hartlén

Tom Spink said:
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.
 
T

Tom Spink

Peter said:
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.

Hi,

If you think about that statement the other way round, it could mean that
code generators know that they MUST write code for that member.

The DefaultValue attribute will _cause_ the generation of code for that
member because your code doesn't necessarily set up the default value to be
what you've said it is.

Does this make things clearer?
 

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