Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1 view
Skip to first unread message

Leon_Amirreza

unread,
Dec 29, 2009, 2:24:47 PM12/29/09
to
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.

Jesse Houwing

unread,
Dec 29, 2009, 2:46:40 PM12/29/09
to

You cannot.

--
Jesse Houwing
jesse.houwing at sogeti.nl

Family Tree Mike

unread,
Dec 29, 2009, 5:10:20 PM12/29/09
to

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

--
Mike

Mick Doherty

unread,
Dec 31, 2009, 6:05:05 AM12/31/09
to
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< --------------------------------------------------

--
Mick Doherty
http://dotnetrix.co.uk/nothing.htm

"Leon_Amirreza" <r_ra...@hotmail.com> wrote in message
news:24DA2D74-4053-4774...@microsoft.com...

0 new messages