.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Application Configuration Settings error "is a type but is used like a variable"
 

Application Configuration Settings error "is a type but is used like a variable"

I'm trying to write to an application settings file.  I'm using VS2005 and am following the example found at http://msdn2.microsoft.com/en-us/library/ms171565.aspx

I added a settings file to the application and it modified my app.config as you see below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="WindowsApplication1.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <WindowsApplication1.Settings>
            <setting name="BackColor" serializeAs="String">
                <value />
            </setting>
        </WindowsApplication1.Settings>
    </userSettings>
</configuration>

I then added a simple class as the example shows:

using System.Windows.Forms;
using System.Configuration;
using System.Drawing;

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

I get the following error when I build:

'WindowsApplication1.Settings' is a 'type' but is used like a 'variable'

Any help would be greatly appriaciated

Thank you,

Tonya

tbednarick

You may have to save it to the configuration file at runtime using the ConfigurationManager.

DotNetFun

There are two things you need to do: 1) change Settings to refer to the base indexer and 2) cast the Object to Color (for the get accessor):

public class MyUserSettings : ApplicationSettingsBase
{
    [UserScopedSetting()]
    [DefaultSettingValue("white")]
    public Color BackgroundColor
    {
        get
        {
            // Cast the Object to Color:
            return (Color)base["BackColor"];
        }
        set
        {
            base["BackColor"] = value;
        }
    }
}

DotNetFun

Thanks for your help...I have another question

What do you mean by change Settings to the base indexer? Change it similarly to MyUserSettings?  VS2005 generated a Settings class when I added the application setting for BackColor...  I've made some notes in the code below in red

//---------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//----------------------------------------------------------------------

namespace WindowsApplication1 {
        
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "8.0.0.0")]
  internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
       
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
       
        public static Settings Default {
            get {
                return defaultInstance;
            }
        }
       
        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("255, 224, 192")]
        public global::System.Drawing.Color BackColor {
            get {
                return ((global::System.Drawing.Color)(this["BackColor"]));
                //Should the be:
                //return ((global::System.Drawing.Color)base["BackColor"]);

}
            set {
                this["BackColor"] = value;
                //And this 
                //base["BackColor"] = value;
                           }
        }
    }
}

Either way (with or without the changes in red above)  I get the following error when I continue with the example code shown below

ERROR: Cannot bind to the property 'BackgroundColor' on the target control.
Parameter name: PropertyName

CODE:
public Form1()
        {
            InitializeComponent();
            MyUserSettings myUserSettings = new MyUserSettings();
            this.DataBindings.Add(new Binding("BackgroundColor", myUserSettings, "BackColor"));

        }

 

tbednarick
Why don't you use ConfigurationSettings or ConfigurationManager to retrieve application configuration settings instead?
DotNetFun

Here's what ended up working.  I didn't use the MyUserSettings class at all.  VS2005 builds a class when you add the user or application settings.

To read the settings in app.config I added

Color color = Properties.Settings.Default.BackColor;
this.BackColor = color;

I was able to change the color at runtime by:

Properties.Settings.Default.BackColor = Color.White;
this.BackColor = Properties.Settings.Default.BackColor;

But it didn't save this setting to app.config.  Any suggestions?

Thanks again for all your help.

Tonya

tbednarick

You may have to save it to the configuration file at runtime using the ConfigurationManager.

DotNetFun

Found it:

private void button1_Click(object sender, EventArgs e)
{
      Properties.
Settings.Default.BackColor = Color.White;
     
this.BackColor = Properties.Settings.Default.BackColor;
      Properties.
Settings.Default.Save();
}

Thanks again for all your help.

Tonya

tbednarick

You can use google to search for other answers

Custom Search

More Threads

• to retrieve the list of add/remove programs frm control panel programtically through C#.NET 2005
• Using String to create object of a class in assembly
• Why do I need to peek at a bitmap's palette before drawing it ?
• Using Thread.sleep in C# .Net
• Getting rid of control characters in the code
• FileStream error - Illegal characters in path
• A Funny with List(Of Type).Remove in a For Each Loop???
• Access Denied on FileInfo.Delete()
• Error calling asp.net window from asp application
• Multiple Project Solution and References