Oh My!! - A Look at the My Namespace in Visual Basic 2005 Productivity is one of the major goals of Visual Basic 2005 and with "My" Microsoft may just have hit a home run. Although Visual Basic .NET is just as powerful as C# for building business applications, it did not get the initial push that C# did back at PDC 2000 when Microsoft unveiled .NET. This was not meant to slight Visual Basic and Visual Basic developers, but rather represented the state of the Visual Basic .NET language which was not as far along in the development process as C#. Opponents to the Basic syntax took this and ran with it. Microsoft has tried to attack this misconception but has also caused some of the problem, initially by pushing .NET for Web services development so hard that many developers and managers incorrectly got idea that .NET was primarily for Web services. With Visual Basic 2005 the power of the .NET Framework is fully exposed and the true power of Visual Basic is once again starting to take form and that power is productivity. Whereas C# is about language first and foremost Visual Basic is about language and tools to make the development process faster. | " | My makes retrieving settings and resources that your application requires simply more productive.
| " |
One of the language and tool innovations introduced in Visual Basic 2005 is the My namespace. This may seem like an odd name at first, but Microsoft created My to make it easier to execute common code patterns that developers use in .NET applications. Often these common code patterns, such as reading the text in a file, were made more confusing than necessary simply because the plethora of awesome choices that .NET provides. Although it is great to have many possible ways to accomplish a task, these choices may also lead to confusion for the developers new to the .NET Framework. My supplies shortcuts to some common boilerplate code such as invoking Web services referenced by the application, gaining access to information about the user, the computer, the network, etc. Some of the capabilities of My will result in less code and in some cases using My makes getting at information more intuitive. In this article I will show you some common tasks that the My namespace makes easier and I'll show how you can add to and extend My with your own information. What's in My? My is a shortcut to several categories of information and functionality. My provides rapid access to the following categories of information: - My.Computer - Access to information related to the computer such as file system, network, devices, system information, etc. My.Computer provides access to a number of very important resources including My.Computer.Network, My.Computer.FileSystem, and My.Computer.Printers.
- My.Application - Access to information related to the particular application such as name, version, current directory, etc.
- My.User - Access to information related to the current authenticated user.
- My.Resources - Access to resources used by the application residing in resource files in a strongly typed manner.
- My.Settings - Access to configuration settings of the application in a strongly typed manner.
My Makes Code More Intuitive One of the reasons that Visual Basic is my preferred language for .NET development is that, in my opinion, using English words makes the intent of the code easier to grasp when someone new to the code looks at it for the first time. Software can be expensive to create, but more often then not it costs more to maintain custom software than to create it. I believe using the My namespace will help to make code easier to grasp when a new developer first looks at it. Let's look at a few examples where My cleans up the code. Note that in the remainder of the article I will refer to Visual Basic .NET 2003 as VB7.1 and I will refer to Visual Basic 2005 as VB8. In a Windows Forms application you often need to know which directory the application is running in so you have access to data files, configuration information, etc. In VB7.1 to find the current directory of an application you could use the following function: Public Function GetAppDirectory() as String Return System.IO.Directory.GetCurrentDirectory End Function
Now that isn't difficult nor does it represent a great deal of code yet the code wasn't intuitive. In VB8 the code to get the current directory looks like this: Public Function GetAppDirectory() as String Return My.Application.CurrentDirectory End Function
| & | | 
By: Keith Franklin
Keith Franklin is the Chief Software Architect of a leading .NET consulting firm in Chicago. Keith is a Microsoft MVP, a regular speaker at VSLive!, DevDays, Microsoft launch events, and the Chicago .NET Users Group, which he founded and directs. His close relationship with members of the Microsoft .NET teams, publishers, speakers, and user group community led to his appointment to the Board of Directors of the International .NET Association (INETA) in September 2001. He authored one of the first Visual Basic books devoted to the .NET Framework. He has also authored numerous articles published by leading .NET and Visual Basic publications and is a member of the .NET Developers Journal editorial board.
ka_franklin@empowered.com | Fast Facts | | Learn to use and extend the new My namespace features of Visual Studio .NET 2005 | |
|