Microsoft & .NET.NETProgramming with the My Feature

Programming with the My Feature content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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?

Technologically, My is part code-generating factory, part proxy pattern, and part façade pattern. When the compiler encounters My in code, depending on the feature used, it may substitute My with dynamically generated code or substituted it with the underlying .NET framework code.

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()
End Sub
End Module

To open the executable assembly containing the code above, follow these steps:

  1. Select Start|All Programs|Microsoft Visual Studio 2005|Visual Studio Tools|Visual Studio 2005 Command Prompt.
  2. At the command prompt, type ildasm and press Enter.
  3. In ildasm, select File|Open and navigate to the managed assembly with the .exe extension.
  4. 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()
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
‘ 2: plays the system
‘ 3: same thing done the long way
Dim a As Microsoft.VisualBasic.Devices.Audio = _
New Microsoft.VisualBasic.Devices.Audio()
‘ 4: copy information to the Clipboard
“VB Today on”)
Const source As String = “c:temptest.txt”
Const target As String = “c:tempbackup 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
End If
‘ 7: make a copy of the text file
My.Computer.FileSystem.CopyFile(source, target)
‘ 8: read the current user informatio
‘ 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:

  1. Plays a .way file.
  2. Plays a system sound (in this example, the beep).
  3. Plays a .wav file using the underlying .NET Framework audio class.
  4. Puts a string in the Clipboard, reads it back out, and writes it to the console.
  5. Writes “some text” to a file named c:temptest.txt.
  6. Deletes a named file if it exists.
  7. Makes a backup copy of the text file you created.
  8. Displays the current user in the console.
  9. 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

If you are interested in joining or sponsoring a .NET Users Group, check out

Copyright © 2005 by Paul T. Kimmel. All Rights Reserved.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories