Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Application Settings. A quick Question about User V's Application Scope

I have just looked at an example from Microsoft Social Network on how to use Application Settings namely this one:

public bool IsUSer(SettingsProperty Setting)
        {
            bool flag = Setting.Attributes[typeof(UserScopedSettingAttribute)] is UserScopedSettingAttribute;
            bool flag1 = Setting.Attributes[typeof(ApplicationScopedSettingAttribute)] is ApplicationScopedSettingAttribute;

            if (flag && flag2)
            {
                throw new ConfigurationErrorsException(SR.GetString("BothScopeAttributes"));
            }
            if (!flag && !flag2)
            {
                throw new ConfigurationErrorsException(SR.GetString("NoScopeAttributes"));
            }
            return flag;
        }

This checks to see if the Setting is both User and Application Scoped or neither. Is it even possible that these two situations can occur. Surely the API would not allow this to happen in the first place. Should we be checking for this or is this example a little over the top. I know this is more a discussional rant but really, surely this can’t really happen?

Link to Example: https://social.msdn.microsoft.com/Forums/en-US/4c0d2eae-2f0b-41c8-bb60-c4b0ffd3cd0b/how-to-retrieve-usersettings-v-defaultsettings-c-vbe2008?forum=netfxbcl

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks
Danny

>Solution :

[UserScopedSetting] and [ApplicationScopedSetting] are attributes so you might use them like this:

public class MyUserSettings : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            return ((Color)this["BackgroundColor"]);
        }
        set
        {
            this["BackgroundColor"] = (Color)value;
        }
    }
}

(Source for above code)

If you refer to this question, you’ll see that it isn’t possible to prevent two attributes occurring together. So, the code you have shown is actually protecting against one of these two scenarios:

1 – Both attributes are applied:

public class MyUserSettings : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [ApplicationScopedSetting()]
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            return ((Color)this["BackgroundColor"]);
        }
        set
        {
            this["BackgroundColor"] = (Color)value;
        }
    }
}

2 – Neither attribute is applied:

public class MyUserSettings : ApplicationSettingsBase
{
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            return ((Color)this["BackgroundColor"]);
        }
        set
        {
            this["BackgroundColor"] = (Color)value;
        }
    }
}

So ultimately it is checking that exactly one of these two attributes is applied.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading