views:

62

answers:

3

What should I type (in both cases) in my app.config's applicationSettings section so that, when I read Settings, I can get the following:

  1. Settings.Default.FooString == null
  2. Settings.Default.FooString == string.Empty

? I guess there is no way to achieve both situations, or am I missing something?

Diffrerenciating whether string value is null or empty is unfortunately required, because this value goes to different system, that takes diffrent decisions upon these two. thanks


further info:

Settings.Designer.cs is being regenerated each time you modify the settings tab. The sample generated setting is:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public string Foo {
        get {
            return ((string)(this["Foo "]));
        }
        set {
            this["Foo "] = value;
        }
    }

the problem is with generated [DefaultSettingValueAttribute("")]. It works as I want when I correct it by hand with [DefaultSettingValueAttribute(null)], however, I would need to do this after whenever I modify any thing on the settings tab, which is unacceptable.

any clues?

+2  A: 

leave the appSetting undefined if you want null, use value="" if you want String.Empty

matt-dot-net
It isn't realted with <appSettings>, It is <applicationSettings>
Łukasz Podolak
sorry... i think it's the same principle though.
matt-dot-net
+2  A: 

You're right, there is no way to achieve both situations, because null != String.Empty.

But you could use Boolean String.IsNullOrEmpty(String) to check for both situations.

Bobby
+2  A: 

For a null, you simply omit the whole <value /> element.

For a string.Empty you use <value /> or <value></value>.

Oded
same answer as above, you do not have any <add /> elements in applicationsettings
Łukasz Podolak
@Łukasz Podolak - I guess both @matt-dot-net and I thought you meant the `appSettings` element. Answer updated.
Oded