PC Review


Reply
Thread Tools Rate Thread

Difficult Question: Assigning string default values to class/structConfigurationProperty attributes

 
 
Keller
Guest
Posts: n/a
 
      27th Apr 2009
Hello Group,

I posted this in microsoft.public.dotnet.general about a week ago but
haven't had any replies....

The Visual Studio 2005 help for
ConfigurationPropertyAttribute.DefaultValue
provides the following sample:

/////////////////////////////////////////////////
[ConfigurationProperty("maxIdleTime",
DefaultValue = "0:10:0",
IsRequired = false)]
[TimeSpanValidator(MinValueString = "0:0:30",
MaxValueString = "5:00:0",
ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}

/////////////////////////////////////////////////

What I am wondering is how does the DefaultValue parameter from the
sample code work, i.e.:

>> DefaultValue = "0:10:0",


TimeSpan does not have a constructor or "=" operator that takes a
string parameter, and TimeSpan does not implement IConvertible. For
example, the following does not complile:

>> TimeSpan ts = "0:10:0";


TimeSpan does have a Parse() method that takes a string parameter,
i.e.

>> public static TimeSpan Parse(string s);


but I do not see how it is associated in the above sample code.

So again how does this code from the sample work in regards to
assigning a string
DefaultValue to a TimeSpan value? i.e.:

>> DefaultValue = "0:10:0",


My guess is TimeSpan.Parse() is somehow being called, but where?

TIA,
Keller Beyer
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Pavel Minaev
Guest
Posts: n/a
 
      27th Apr 2009
On Apr 26, 9:43*pm, Keller <beyer...@aol.com> wrote:
> I posted this in microsoft.public.dotnet.general about a week ago but
> haven't had any replies....
>
> The Visual Studio 2005 help for
> ConfigurationPropertyAttribute.DefaultValue
> provides the following sample:
>
> /////////////////////////////////////////////////
> [ConfigurationProperty("maxIdleTime",
> * * DefaultValue = "0:10:0",
> * * IsRequired = false)]
> [TimeSpanValidator(MinValueString = "0:0:30",
> * * MaxValueString = "5:00:0",
> * * ExcludeRange = false)]
> public TimeSpan MaxIdleTime
> {
> * * get
> * * {
> * * * * return (TimeSpan)this["maxIdleTime"];
> * * }
> * * set
> * * {
> * * * * this["maxIdleTime"] = value;
> * * }
>
> }
>
> /////////////////////////////////////////////////
>
> What I am wondering is how does the DefaultValue parameter from the
> sample code work, i.e.:
>
> >> * DefaultValue = "0:10:0",

>
> TimeSpan does not have a constructor or "=" operator that takes a
> string parameter, and TimeSpan does not implement IConvertible. *For
> example, the following does not complile:
>
> >> * TimeSpan ts = "0:10:0";

>
> TimeSpan does have a Parse() method that takes a string parameter,
> i.e.
>
> >> public static TimeSpan Parse(string s);

>
> but I do not see how it is associated in the above sample code.
>
> So again how does this code from the sample work in regards to
> assigning a string
> DefaultValue to a TimeSpan value? i.e.:
>
> >> * DefaultValue = "0:10:0",

>
> My guess is TimeSpan.Parse() is somehow being called, but where?


I am not familiar with this attribute in particular, but the general
way to do this sort of thing in .NET libraries is to use a type
converter. TimeSpan has an associated TimeSpanConverter, which
converts to/from string. It can be obtained using
TimeConverter.GetConverter(typeof(TimeSpan)), which is probably what
happens here.
 
Reply With Quote
 
Keller
Guest
Posts: n/a
 
      27th Apr 2009
On Apr 27, 2:00*am, Pavel Minaev <int...@gmail.com> wrote:
> On Apr 26, 9:43*pm, Keller <beyer...@aol.com> wrote:
>
>
>
>
>
> > I posted this in microsoft.public.dotnet.general about a week ago but
> > haven't had any replies....

>
> > The Visual Studio 2005 help for
> > ConfigurationPropertyAttribute.DefaultValue
> > provides the following sample:

>
> > /////////////////////////////////////////////////
> > [ConfigurationProperty("maxIdleTime",
> > * * DefaultValue = "0:10:0",
> > * * IsRequired = false)]
> > [TimeSpanValidator(MinValueString = "0:0:30",
> > * * MaxValueString = "5:00:0",
> > * * ExcludeRange = false)]
> > public TimeSpan MaxIdleTime
> > {
> > * * get
> > * * {
> > * * * * return (TimeSpan)this["maxIdleTime"];
> > * * }
> > * * set
> > * * {
> > * * * * this["maxIdleTime"] = value;
> > * * }

>
> > }

>
> > /////////////////////////////////////////////////

>
> > What I am wondering is how does the DefaultValue parameter from the
> > sample code work, i.e.:

>
> > >> * DefaultValue = "0:10:0",

>
> > TimeSpan does not have a constructor or "=" operator that takes a
> > string parameter, and TimeSpan does not implement IConvertible. *For
> > example, the following does not complile:

>
> > >> * TimeSpan ts = "0:10:0";

>
> > TimeSpan does have a Parse() method that takes a string parameter,
> > i.e.

>
> > >> public static TimeSpan Parse(string s);

>
> > but I do not see how it is associated in the above sample code.

>
> > So again how does this code from the sample work in regards to
> > assigning a string
> > DefaultValue to a TimeSpan value? i.e.:

>
> > >> * DefaultValue = "0:10:0",

>
> > My guess is TimeSpan.Parse() is somehow being called, but where?

>
> I am not familiar with this attribute in particular, but the general
> way to do this sort of thing in .NET libraries is to use a type
> converter. TimeSpan has an associated TimeSpanConverter, which
> converts to/from string. It can be obtained using
> TimeConverter.GetConverter(typeof(TimeSpan)), which is probably what
> happens here.- Hide quoted text -
>
> - Show quoted text -


Hi Pavel,
Thanks for the help and info regarding TimeSpanConverter. I will post
what I learn about how this example works.
Keller
 
Reply With Quote
 
Keller
Guest
Posts: n/a
 
      27th Apr 2009
On Apr 27, 2:28*am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Sun, 26 Apr 2009 21:43:26 -0700, Keller <beyer...@aol.com> wrote:
> > [...]
> >>> * DefaultValue = "0:10:0",

>
> > My guess is TimeSpan.Parse() is somehow being called, but where?

>
> Like Pavel, I don't have a specific answer for you. *But, note that you*
> can inspect the .NET implementation yourself and see how it works. *With *
> Red Gate's Reflector, you can browse around the .NET DLLs. *An easier way *
> is to enable source-code download in Visual Studio, set a break-point on *
> the property's setter, and then see what led up to the default value being *
> set.
>
> Note that TimeSpan is one of five special-cased types supported for *
> configuration elements (Int32, Int64, regex String, and plain String being *
> the other four). *So it wouldn't surprise me too much to see some code in *
> the configuration-management implementation that simply special-cases the*
> type and performs the necessary Parse() (or possibly the TimeSpanConverter *
> as Pavel suggests).
>
> Pete


Hi Pete,
Thanks for the help and info about the special-cased types.

I did set a break point on the property's setter, but I don't think it
gets called (I will double check tonight). i.e.:

public class ConfigurationSection :
System.Configuration.ConfigurationSection
{
[ConfigurationProperty("maxDateTime", DefaultValue =
"0:10:0")]
public System.DateTime MaxDateTime
{
get { return (System.DateTime)this
["maxDateTime"]; }
set { this["maxDateTime"] = value; } // << Does
not appear to be called by constructor to set DefaultValue
}

[ConfigurationProperty("maxIdleTime", DefaultValue =
"0:10:0")]
public TimeSpan MaxIdleTime
{
get { return (TimeSpan)this["maxIdleTime"]; }
set { this["maxIdleTime"] = value; } // << Does
not appear to be called by constructor to set DefaultValue
}
}

I have been meaning to install Reflector, but I want to do a drive
image of my system first (I also have the
mobile SDK installed). I also plan to install the "Configuration
Section Designer" tool available on CodePlex,
i.e. http://csd.codeplex.com/.

Thanks again for the reply, I will post what I learn about setting
DefaultValues.
Keller




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assigning string values to a range Risky Dave Microsoft Excel Programming 4 15th Aug 2008 02:27 AM
question about attributes of class André Microsoft VB .NET 2 16th Mar 2007 07:14 AM
Assigning Description Attributes to class methods Sergey Poberezovskiy Microsoft VB .NET 3 4th Nov 2003 03:59 AM
Values differ after assigning 1 string to another Stephen Graybeal Microsoft VB .NET 0 21st Aug 2003 03:50 AM
newbie: Loading XML and storing values of attributes in a String abdul bari Microsoft C# .NET 1 6th Aug 2003 03:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:10 PM.