Parsing boolean from configuration section in web.config
- by Bloopy
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!