C#: How to make sure a settings variable exists before attempting to use it from another assembly?
Asked Answered
K

5

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);
    }
}
Kitchens answered 10/1, 2011 at 14:27 Comment(1)
msdn.microsoft.com/en-us/library/…Inerrant
M
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]);
}
Mihrab answered 10/1, 2011 at 14:29 Comment(5)
I thought he was more asking how to ensure that the setting is present, not just to check whether it is present.Belisle
@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.Mihrab
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.Kitchens
This doesn't work with Properties.Settings.Default. @Salle55 provided a good solution that does work with Properties.Settings.Default below.Rabjohn
For me: CommonSettings.Default.Context.ContainsKey(str) (VS 2019, .NET4.5)Membership
L
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);
}
Lignite answered 9/1, 2014 at 13:2 Comment(1)
thx salle55. great work. For those that don't have using System.Configuration please add prefix System.Configuration. to SettingsProperty>Rancor
M
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]);
}
Mihrab answered 10/1, 2011 at 14:29 Comment(5)
I thought he was more asking how to ensure that the setting is present, not just to check whether it is present.Belisle
@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.Mihrab
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.Kitchens
This doesn't work with Properties.Settings.Default. @Salle55 provided a good solution that does work with Properties.Settings.Default below.Rabjohn
For me: CommonSettings.Default.Context.ContainsKey(str) (VS 2019, .NET4.5)Membership
M
6
try
{
    var x = Settings.Default[bonusMalusTypeKey]);
}
catch (SettingsPropertyNotFoundException ex)
{
    // Ignore this exception (return default value that was set)
}
Matt answered 3/12, 2011 at 19:33 Comment(1)
Some description would be useful!Analyze
C
5

This is how you deal with it:

if(CommonSettings.Default.Properties[str] != null)
{
    //Hooray, we found it!
}
else
{
    //This is a 'no go'
}
Currier answered 6/7, 2011 at 20:23 Comment(1)
this condition will be true even you haven't set it before. I think OP wanted to know this settings have set before.Formality
C
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)

Cammiecammy answered 10/1, 2011 at 14:39 Comment(1)
Just seen your update Justin - Your solution looks allot cleaner :)Cammiecammy

© 2022 - 2024 — McMap. All rights reserved.