Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9173345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:34:05+00:00 2026-06-17T16:34:05+00:00

I have created a custom configuration section with the following property: private const string

  • 0

I have created a custom configuration section with the following property:

private const string UseMediaServerKey = "useMediaServer";
[ConfigurationProperty(UseMediaServerKey, IsRequired = false, DefaultValue = false)]
public bool UseMediaServer
{
    get { return bool.Parse(this[UseMediaServerKey] as string); }
    set { this[UseMediaServerKey] = value; }
}

My understanding is that if the property is not defined in the configuration file then the DefaultValue should be returned.

However, in the above case a ArgumentNullException is thrown at bool.Parse(...) meaning the default accessor is executed even when the configuration property is not defined.

Of course I could change the property accessor to:

    private const string UseMediaServerKey = "useMediaServer";
    [ConfigurationProperty(UseMediaServerKey, IsRequired = false)]
    public bool UseMediaServer
    {
        get {
            bool result;
            if (bool.TryParse(this[UseMediaServerKey] as string, out result))
            {
                return result;
            }

            return false;
        }
        set { this[UseMediaServerKey] = value; }
    }

But then, what’s the point of the DefaultValue property?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:34:06+00:00Added an answer on June 17, 2026 at 4:34 pm

    this[UseMediaServerKey] as string is null because the value is of type bool, not string. You don’t have to do any string conversion in a custom configuration section: everything is handled for you by the framework.

    Simplify your code to:

    public bool UseMediaServer
    {
        get { return (bool) this[UseMediaServerKey]; }
        set { this[UseMediaServerKey] = value; }
    }
    

    And you’re done. this[UserMediaServerKey] will return the DefaultValue correctly typed if there is none in the configuration file. If you ever had to change the string conversion process, put a TypeConverterAttribute on the configuration property. But that isn’t necessary here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just rolled a custom configuration section, created an accompanying schema document for
i have created the new custom config TAB in admin panel configuration section under
I have created a custom configuration section in a c# class library by inheriting
I have created a configuration section designer project to represent nodes of a custom
I am writing my own custom configuration section and have a ConfigurationProperty defined in
I have created a custom configuration section like below <configSections> </configSections> <Tabs> <Tab name="Dashboard"
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I have created a custom control but I can't bind a property to the
I have created a custom config section to store a list of of items.
I have created a custom module which displays a tab and a section in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.