How can I have [DefaultValue(Environment.NewLine)] in C#?

L

Leon_Amirreza

hi
How can I have this in c# (or any equivalent code that does the same thing):

[DefaultValue(Environment.NewLine)]

or

[DefaultValue(f())]

f() is a function that returns a value.
 
J

Jesse Houwing

* Leon_Amirreza wrote, On 29-12-2009 20:24:
hi
How can I have this in c# (or any equivalent code that does the same
thing):

[DefaultValue(Environment.NewLine)]

or

[DefaultValue(f())]

f() is a function that returns a value.

You cannot.
 
F

Family Tree Mike

hi
How can I have this in c# (or any equivalent code that does the same
thing):

[DefaultValue(Environment.NewLine)]

or

[DefaultValue(f())]

f() is a function that returns a value.

You can set those values for the properties in the constructor.
 
M

Mick Doherty

The DefaultValueAttribute basically just determines a value which does not
need to be serialized and which will be set upon a reset command. It does
not set the initial value, this needs to be done by yourself.

To do this with a non fixed value you should define ShouldSerialize* and
Reset* methods instead, where * is the property name.

i.e.
-- 8< --------------------------------------------------

private string someProperty = Environment.NewLine;
public string SomeProperty
{
get{ return someProperty; }
set{ someProperty = value; }
}
private bool ShouldSerializeSomeProperty()
{
return !SomeProperty.Equals(Environment.NewLine);
}
private void ResetSomeProperty()
{
SomeProperty = Environment.NewLine;
}

-- 8< --------------------------------------------------
 

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