Blib...Blog...Blib...

Thursday, August 24, 2006

C# 2.0 Settings Quirk

One of the nice new features in C# 2.0/VS2005 is the visual settings editor for persisting application and user settings between sessions.

This post is not a discussion about this facility, it is an observation of a problem that can occur and a way to solve it.

If you have a custom class in your project, and you want to persist an instance of this, you would do the following:

In the Settings tab in project properties :-
  • Enter a Name (e.g. SavedMyClass)
  • Choose Browse... in the Type column
  • In the 'Select a Type' dialog, type your fully qualified class name (e.g. WindowsApplication1.MyClass)
  • Set Scope to 'User'

And you have now created a strongly typed setting in Properties.Settings.Default.SavedMyClass that you can access on application startup and will be saved during a call to Properties.Settings.Default.Save().

The problem occurs when you build the app. All being well, the build will succeed; but you should see many warnings like the following:

The type 'WindowsApplication1.MyClass' in 'C:\Documents and Settings\carlw\My Documents\Visual Studio 2005\Projects\WindowsApplication1\Form1.cs' conflicts with the imported type 'WindowsApplication1.MyClass' in 'C:\Documents and Settings\carlw\My Documents\Visual Studio 2005\Projects\WindowsApplication1\bin\Debug\WindowsApplication1.exe'. Using the one in 'Form1.cs'

The reason for this is that the output exe from this project is automatically added to the References for the project, thus creating the conflict.

This is easily solved by just removing this reference (e.g. WindowsApplication1) from the references in the project, although this may not be an immediately obvious solution as you would (or at least I did) expect this reference to be recognised is local.

Just incase anyone comes across this.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home