TypeConverters and null
<p>I am using a class to store connection strings. My app reads a setting from the settings class and assigns it to an instance variable. This is then bound to some controls. My connection string class has the following attribute set:</p>

<pre><code>[TypeConverter(typeof(ConnectionStringConverter))]
</code></pre>

<p>My type convertor looks like is below.</p>

<p>The problem is that if the settings in the settings file is blank the settings class returns null. and not an instance of my connection string class using the default constructor.</p>

<p>Please could someone help me in solving this riddle.</p>

<p>Thanks.</p>

<pre><code>public class ConnectionStringConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        if (sourceType == typeof(string))
            return true;
        else
            return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string)
            return (ConnectionString)(value as string);
        else
            return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
            return (string)(value as ConnectionString);
        else
            return base.ConvertTo(context, culture, value, destinationType);
    }
}
</code></pre>
