Using project settings files and global::

Okay… this is not the posting I was planning on making this week, but I learned something new in .NET 2.0 that I thought was outrageously cool and I have never seen before. Here it is:
 
Sometimes there are application constants that you want to be able to set up that are not used in a user interface. Things like the URL to your web service where, though you could put it into the Web.Config, it just wouldn’t be the right thing to do as it’s not something you want to be configurable. Imagine the following scenario: You are building an assembly that contains your web service proxy classes. You don’t want clients to inadvertently change this setting to something incorrect and then have them call to tell you that your application is not working… argh!
 
So, you create a new Settings file (Add > New Item > Settings File) for your project (yeah, this was the first time I ever noticed that item in the create new item templates myself!). This will create the file in the Properties folderf your project. Just like a strongly typed resource, you can add entries into the file that is simply an XML file (behind the scenes). To access the settings from code, you use something similar to the following:
 

string webServiceUrl =

global::MyNamespace.Properties.Settings.Default.WebServiceUrl;

 
If you are wanting to change this for different deployment environments (re: the scenario I mentioned previously) and are using a built tool such as Nant, you can simply repace the text prior to building the assembly.
 
I have not been able to find any documentation on the use of the global keyword along with what I can only assume is a scope resolution operator (which I have only ever seen used in c++). If you’ve seen this, I’d lvoe to read more about it. Post a comment.
 
After stumbling across this feature I googled the settings file info and found this great article, by Rob Howard, descibing the usage of this feature. Awesome!
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s