My.Settings is a very useful new aspect of the .NET Framework 2.0. It is worthwhile to view this element of the My extensions in context, along with its other brethren. In fact, if there's an underappreciated workhorse in the new Visual Studio 2005 (and its equally new companion, the .NET Framework 2.0) it's got to be the many Visual Basic language extensions that fall under the My hierarchy in that environment. You can use My constructs for an amazing array of activities, as you'll shortly see.
A brief run-down on what this do-it-all namespace can do includes the following:
A good example of what's possible may be revealed by exploring the My.Computer.FileSystem and other related classes, which some experts describe as a "speed-dial" for the .NET framework. For example, whereas some programmers may choose to use IO.StreamReader and its sr.ReadToEnd function to copy the contents of a text file into a string (which requires at least three lines of code), a single assignment statement using My will also do the job -- namely:
Contents = My.Computer.FileSystem.ReadAllText("c:\mytextfile.txt")
Understanding
Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.
Hannah Smalltree, Editorial Director- the basic organization of classes within My really helps to get a sense of the kinds of uses to which these language extensions are suited:
- My.Application: This offers information about application information and services available, including a built-in logging facility.
- My.Computer: Along with My.User, this permits easy navigation of services and data for the current host computer (this is where the FileSystem classes come into play, and where My.Computer.Ports, My.Computer.Network, and My.Computer.Printer are also available).
- My.Forms: Use this to navigate and interact with forms available to your project.
- My.Resources: Use this to inspect or interact with resources configured using other VB or VS.NET tools in advance, or added to your project.
- My.Settings: Use this to inspect or interact with settings configured using other VB or VS.NET tools in advance, or added to your project.
- My.User: Along with My.Computer, this permits easy navigation of services and data for the current host computer.
- My.WebServices: Use this to navigate and interact with Web services referenced in a project.
There's so much going on here that it will take some time to explore and learn the many capabilities (and time-saving techniques) available through My. You'll also find these MSDN articles helpful in that quest:
- Visual Basic Developer Center Navigate the .NET Framework and Your Projects with "My" by Duncan Mackenzie.
- Visual Studio 2005 Technical Articles My.Internals: Examining the Visual Basic My Feature
But if you put in the time and effort, you can count on reaping some righteous rewards!
Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed at etittel@techtarget.com with comments, questions, or suggested topics or tools to review.
This was first published in November 2005