Parsing boolean from configuration section in web.config

Posted by Bloopy on Stack Overflow See other posts from Stack Overflow or by Bloopy
Published on 2009-11-04T17:39:52Z Indexed on 2010/04/07 2:53 UTC
Read the original article Hit count: 293

Filed under:
|

I have a custom configuration section in my web.config.

One of my classes is grabbing from this:

<myConfigSection LabelVisible="" TitleVisible="true"/>

I have things working for parsing if I have true or false, however if the attribute is blank I am getting errors. When the config section tries to map the class to the configuration section I get an error of "not a valid value for bool" on the 'LabelVisible' part.

How can I parse "" as false in my myConfigSection class?

I have tried this:

    [ConfigurationProperty("labelsVisible", DefaultValue = true, IsRequired = false)]
    public bool? LabelsVisible
    {
        get
        {

            return (bool?)this["labelsVisible"];

        }

But when I try and use what is returned like so:

graph.Label.Visible = myConfigSection.LabelsVisible;

I get an error of:

'Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)  

Thanks for any suggestions!

© Stack Overflow or respective owner

Related posts about c#

Related posts about nullable-types