17

I have the following:

using CommonSettings = MyProject.Commons.Settings;

public class Foo
{
    public static void DoSomething(string str)
    {
        //How do I make sure that the setting exists first?
        object setting = CommonSettings.Default[str];

        DoSomethingElse(setting);
    }
}
michael
  • 14,844
  • 28
  • 89
  • 177

5 Answers5

30

If you are using a SettingsPropertyCollection you have to loop and check which settings exists yourself it seems, since it doesn't have any Contains-method.

private bool DoesSettingExist(string settingName)
{
   return Properties.Settings.Default.Properties.Cast<SettingsProperty>().Any(prop => prop.Name == settingName);
}
salle55
  • 2,101
  • 2
  • 25
  • 27
  • 2
    thx salle55. great work. For those that don't have using System.Configuration please add prefix System.Configuration. to SettingsProperty> – gg89 Jan 12 '17 at 06:42
6

Depending on what type CommomSettings.Default is, a simple null check should be fine:

if(setting != null)
    DoSomethingElse(setting);

If you want to check BEFORE trying to retrieve the setting, you need to post the Type of CommonSettings.Default. It looks like a Dictionary so you might be able to get away with:

if(CommonSettings.Default.ContainsKey(str))
{
    DoSomethingElse(CommonSettings.Default[str]);
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • I thought he was more asking how to _ensure_ that the setting is present, not just to check whether it is present. – Uwe Keim Jan 10 '11 at 14:31
  • 1
    @Uwe - You might be right. I couldn't tell from the wording in the OP's question. I added something for that case as well. – Justin Niessner Jan 10 '11 at 14:35
  • 1
    It is however a Settings.settings file is constructed... I don't think it's a Dictionary type since `CommonSettings.Default.ContainsKey(...)` is not in the intellisense. – michael Jan 10 '11 at 16:24
  • 2
    This doesn't work with Properties.Settings.Default. @Salle55 provided a good solution that does work with Properties.Settings.Default below. – Farrah Stark Jun 12 '14 at 18:11
  • For me: `CommonSettings.Default.Context.ContainsKey(str)` (VS 2019, .NET4.5) – David Silva-Barrera Aug 12 '21 at 10:28
6
try
{
    var x = Settings.Default[bonusMalusTypeKey]);
}
catch (SettingsPropertyNotFoundException ex)
{
    // Ignore this exception (return default value that was set)
}
animuson
  • 53,861
  • 28
  • 137
  • 147
ajoka
  • 61
  • 1
  • 1
5

This is how you deal with it:

if(CommonSettings.Default.Properties[str] != null)
{
    //Hooray, we found it!
}
else
{
    //This is a 'no go'
}
DarthJDG
  • 16,511
  • 11
  • 49
  • 56
André Filipe
  • 67
  • 1
  • 1
  • 1
    this condition will be true even you haven't set it before. I think OP wanted to know this settings have set before. – Javidan May 24 '18 at 20:11
0

You could do the following:

public static void DoSomething(string str)
{
    object setting = null;

    Try
    {
        setting = CommonSettings.Default[str];
    }
    catch(Exception ex)
    {
        Console.out.write(ex.Message);
    }

    if(setting != null)
    {
        DoSomethingElse(setting);
    }
}

This would ensure the setting exists - you could go a bit further and try and catch the exact excetion - e.g catch(IndexOutOfBoundsException ex)

GaryT
  • 115
  • 1
  • 1
  • 8