Programming with the My Feature
The keyword My is what is often referred to as syntactic sugar. My and keywords like it exist to make things a little easier for programmers. In particular, My makes VB.NET a little simpler to use, which conceptually falls in line what VB is all about. First, VB made rapid application development a reality. Next, VB helped kill IDL (Interface Definition Language) and made COM easier to use, and now with My, VB is making the .NET Framework easier to use. The .NET Framework is a large, powerful framework, and making it easier to use is an important contribution to programming.
After some debate as to what My was exactly, Microsoft seems to have settled on calling it a feature. My acts as a cue for the VB compiler to substitute it with more verbose code that creates an object and then invokes a method on that object. But, that doesn't really matter as much as the results. Neither does the fact that a class called the GeneratedCodeAttribute is associated with My. (However, these are important to me because I love obscure facts that can be gleaned only through secret relationships or by digging around with tools like ILDASM or Reflector.)
What Is My?
You or I could simulate (and probably have simulated) My by creating wrapper classes with static methods that make it easier to invoke underlying behaviors. However, now that My exists, we don't need to re-invent the wheel.
What is ironic about My is that it brings us full circle because it is effectively procedural code, albeit highly organized procedural code.
Well, enough foreplay. Look at some examples.
Things You Can Do Easily with My
If you want to figure out what the underlying .NET Framework code is for a particular My behavior, open any compiled assembly containing code that uses My with the MSIL Disassembler (ildasm.exe) tool. (ildasm.exe is a utility that ships with .NET.) Listing 1 provides a simple example that plays a .wav sound file with My and a single line of code.
Listing 1: Playing a .Wav Sound File with My and a Single Line of Code
Module Module1 Sub Main() My.Computer.Audio.Play("c:\windows\media\chimes.wav") End Sub End Module
To open the executable assembly containing the code above, follow these steps:
- Select Start|All Programs|Microsoft Visual Studio 2005|Visual Studio Tools|Visual Studio 2005 Command Prompt.
- At the command prompt, type ildasm and press Enter.
- In ildasm, select File|Open and navigate to the managed assembly with the .exe extension.
- Expand the view until you see the Main node, and double-click the node to expand it.
Figure 1 shows the corresponding MSIL for Listing 1 through the ildasm.exe tool.
Figure 1: MSIL Disassembler View of Compiled Assembly
It's a bit of a stretch, but you can see the Constructor Call (IL_0005), the .wav File String Declaration (IL_000a), and the Call to Play in Figure 1's MSIL.
Building on the same object and the MSIL shown in Figure 1, you can trace the My.Computer.Audio.Play method invocation to its underlying framework classes and methods. Listing 2 shows the long version of Listing 1.
Listing 2: Playing a .wav File Using the Audio Class and Play Method
Module Module1 Sub Main() '' same thing done the long way Dim a As Microsoft.VisualBasic.Devices.Audio = _ New Microsoft.VisualBasic.Devices.Audio() a.Play("c:\windows\media\chimes.wav") End Sub End Module
Taken to its absurd end, the My concept offers a huge framework of highly organized procedural calls. But, if My is successful at aiding in productivity and selling copies of VB.NET, you will probably see many more .NET Framework elements wrapped up in My.
My Features in Action
My supports several easy-to-use features, including the following:
- Moving information to and from the Clipboard
- Copying files
- Obtaining user information
- Writing information to the default log listener
Listing 3 provides several statements that demonstrate examples of the My features.
Listing 3: Examples of Features Provided by My
Module Module1 Sub Main() ' 1: plays the specified .wav file My.Computer.Audio.Play("c:\windows\media\chimes.wav") ' 2: plays the system My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep) ' 3: same thing done the long way Dim a As Microsoft.VisualBasic.Devices.Audio = _ New Microsoft.VisualBasic.Devices.Audio() a.Play("c:\windows\media\chimes.wav") ' 4: copy information to the Clipboard My.Computer.Clipboard.SetData("String", "VB Today on codeguru.com") Console.WriteLine(My.Computer.Clipboard.GetData("String")) Const source As String = "c:\temp\test.txt" Const target As String = "c:\temp\backup test.txt" ' 5: creates a text file My.Computer.FileSystem.WriteAllText(source, "Some text", True) ' 6: delete a file if it exists If (My.Computer.FileSystem.FileExists(target)) Then My.Computer.FileSystem.DeleteFile(target) End If ' 7: make a copy of the text file My.Computer.FileSystem.CopyFile(source, target) ' 8: read the current user informatio Console.WriteLine(My.User.Name) ' 9: write to the default log listener, ' which is the output Window ' implpement a TraceListener to change the log location My.Application.Log.WriteEntry("My is cool!") End Sub End Module
The statements in Listing 3 are numbered for reference and perform the following tasks:
- Plays a .way file.
- Plays a system sound (in this example, the beep).
- Plays a .wav file using the underlying .NET Framework audio class.
- Puts a string in the Clipboard, reads it back out, and writes it to the console.
- Writes "some text" to a file named c:\temp\test.txt.
- Deletes a named file if it exists.
- Makes a backup copy of the text file you created.
- Displays the current user in the console.
- Writes the text to the default log listener. (A log listener is a class that inherits from the tracelistener class. The default log listener is the ide's output window.)
It's All About Ease
If you finished this article and thought "gee, that was easy!" I guess Microsoft's VB implementers have done their job. My is supposed to make programming with VB.NET easier, and in some areas it does just that. VB is supposed to make programming in the latest technologies easier and that seems to the focus of our friends at Microsoft.
What do you want to program today?
About the Author
Paul Kimmel has written several books on object-oriented programming and .NET. Check out his new book UML DeMystified from McGraw-Hill/Osborne and his upcoming book C# Express from Addison Wesley (Spring 2006). Paul is an architect for Tri-State Hospital Supply Corporation. You may contact him for technology questions at email@example.com.
If you are interested in joining or sponsoring a .NET Users Group, check out www.glugnet.org.
Copyright © 2005 by Paul T. Kimmel. All Rights Reserved.