Ziff Davis EnterpriseDevLife
Advertisement

Monday, January 21, 2008 1:13 PM/EST

Sharing application settings across projects in VS2005 (and VS2008)

Kathleen Dollard's recent "Ask Kathleen" column in Visual Studio Magazine talks about an improvement in VS2008 IDE that makes it easier to share Settings and Resources by allowing you to set the scope (which in VS2005 is Friend) to Friend OR Public. She then goes on to focus on the issue of Namespaces once you have the settings created in a way that can be shared.

When the settings are Public, you can easily access them from any project that has a reference to the project where the settings live.

In Visual Studio 2005, however, this is more difficult, because the settings are always marked as Friend.

In this case, Kathleen suggests (with caveats) is to just edit the generated settings class and change the scope. This is something I don't like to do, so I thought I would share my solution for sharing settings.

In my solution, I create a project that is just for settings and resources. In a big app, I might split these into two separate projects.

After creating my settings, for example:


I create a wrapper class which I use to expose the settings, including the user settings which can be updated.

Public Class Common

  Public Shared ReadOnly Property ConnString() As String

    Get

      Return My.Settings.connString.ToString

    End Get

  End Property

  Public Shared ReadOnly Property MessageBoxTitle() As String

    Get

      Return My.Settings.MessageBoxTitle

    End Get

  End Property

  Public Shared Property UserColor() As System.Drawing.Color

    Get

      Return My.Settings.UserColor

    End Get

    Set(ByVal value As System.Drawing.Color)

      My.Settings.UserColor = value

      My.Settings.Save()

    End Set

  End Property

End Class

The properties are Shared so that there's no need to instantiate the class when I want to use it.

Now when I am in another project that has a reference to my AppSettings project, I have easy access to my settings.


TrackBack

TrackBack

http://blogs.devsource.com/cgi-bin/mte/mt-tb.cgi/12522

Post a Comment

 
 

Advertisement

Syndication

Subscribe: