Julie Lerman's DevLife

DevLife Part I [May 2005 - March 2007]

My Links

Blog Stats

News

A blog for DevSource.com.

This blog was originally part of the blogs.ziffdavis.com site from May 2005 through June 2007 when the blog was moved to the Movable Type blog engine and hosted at blog.devsource.com/devlife.
The original blog was eventually shut down and I was given the posts so that I could host them on my own site.


Archives

Accessing VS2005 Resources and Settings the easy way in C#

I cheated. I used the My namespace to get easy access to the resources and settings. So shoot me. It was so easy. My.Resources gives me strongly typed intellisense on all of the items I have added into the projects Resources. My.Settings does the same for project settings. Of course, this is a VB thing. If you are a C# programmer, you probably are saying “I don't need no stinkin' My namespace”.

Well, I had to convert some VB code to C# and spent a ridiculous amount of time figuring out how to get at the settings and resources programmatically without using the My shortcut. There was lots of documentation on how to get the stuff INTO settings and resources and how to implement your own classes. But it took a lot of trial and error to replace some simple My.Resources.MyPrettyBMP and My.Settings.MySqlConnection code.

I finally noticed the strongly typed Resource classes in the C# project. Here's the crazy thing about this. Nearly three years ago, when doing talks on “What's new in the .NET 2.0 Base Class Libraries“, I talked about the ability to use a command line tool to build strongly typed classes with the resources. I now remember that there was talk of embedding this directly into VS2005 and making the generation of the classes transparent. But heck, that was a long time ago and what can I say, I forgot. Gulp. So there they were now, some pretty little classes to make getting at my Settings and Resources pretty easy. In VB, they are created with the namespace of “My“ and in C# they get the namespace “Properties“.

Oddly when I finally figured out Settings in C#, the same pattern did not apply to Resources. Uggh. The reason is that the Resources expose static properties but the settings do not. I had to instantiate a settings class to get at my settings. VB makes settings Shared (the VB word for static).

Accessing Settings in VB
myConnString=My.Settings.MySqlConnection

Accessing Settings in C#
Properties.Settings sett = new Properties.Settings(); //note that Properties is a class in my application, not a .NET class.
myConnString= sett.NorthwindConnectionString;

Accessing Resources in VB
myImg=My.Resources.MyPrettyBMP

Accessing Resources in C#
myImg=Properties.Resources.MyPrettyBMP //note that Properties is a class in my application, not a .NET class.

posted on Friday, September 08, 2006 1:42 PM