Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

Retrieve, Change, or Create Program Information

Getting Resources With Reflection

By Dan Mabbutt, About.com

Jun 16 2007

Recently, About Visual Basic reviewed the book, Learn VB.NET Through Game Programming. A more serious example using reflection is part of the first example. You can download this complete program and all of the other examples for the book at the APress web site.

In the GuessTheDieRoll, the visual image of a die rolling across the window is painted using .NET's GDI+ (Graphic Device Interface) objects. But the raw images themselves are included as "embedded resources" in the assembly for the program. This is the third box in the illustration above.

These resources are added to the assembly using the Add Existing Item ... menu item in Solution Explorer and then the property of the resource is changed to Embedded Resource. Once this is done, the bitmap files themselves can be used in the project using this code. (Only one of the three bitmaps is shown.)

Private bmStop As Bitmap

Dim a As Reflection.Assembly = _
   System.Reflection.Assembly.GetExecutingAssembly()
bmStop = New Bitmap(a.GetManifestResourceStream( _
   "GuessTheDieRoll.dicedone.bmp"))

This allows you to use the bmStop bitmap as an object within your program. You can do the same thing with WAV, ICO, or other types of resource files.

The uses of reflection in .NET are just starting to be appreciated by developers. While most VB.NET books barely mention it, a few are now starting to give it the attention it deserves. One of the best is Paul Kimmel's "Visual Basic .NET Unleashed". Paul has included an entire chapter about reflection. But the best book is a slim volume originally published by Wrox, "Visual Basic .NET Reflection Handbook" in 2002. Wrox ran into financial trouble, but support for the book has been assumed by Apress! (You can download the support files from this Apress web site.) You can still find copies of the original book, but Apress has published an update, along with some additional topics, from the cast of original authors in the book, "Pro .NET 1.1 Remoting, Reflection, and Threading". For the "higher order" uses of reflection, get either of these books.

The My Namespace

VB.NET 2005 introduced an innovation (not available to C# coders ... sorry!) called the My Namespace. I provided an introduction to the My namespace in my tutorial for Visual Basic 2005 Express. An example there shows how to customize the VB.NET "Spashscreen" template using the My namespace.

The My namespace doesn't actually provide completely new capability to your program. You could get the same information in previous versions and in other languages. Before Framework 2.0 was released, I wrote an article showing one way to do it. But the My namespace gives you access to properties and methods in seven groups of software objects more quickly and easily than was possible before. These groups of objects are:

  • My.Application
  • My.Computer
  • My.Forms
  • My.Resources
  • My.Settings
  • My.User
  • My.WebServices

The illustration below shows how VB.NET's Intellisense can point you in the right direction once you have a basic understanding of what's available (the categories above).

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The last item in the illustration above is My.Computer.Ports and it fills a gap that has been missing in VB.NET since VB 6 days - the ability to program serial ports (without calling a Windows API). Microsoft has finally added this capability in .NET 2.0. Together with the System.IO.Ports.SerialPort class, you can now communicate with the whole world of inexpensive devices that work with serial communications.

For example, a lot of home automation "X10" devices work with serial communications. Serial control ports are common on lab equipment. You find them on everything from bar code readers to cash registers. In the last few years, quite a few vendors jumped into the gap left by the lack of support for port communication in .NET and started selling components to do that. But now, Microsoft has made it dead easy. Here's how to read the serial ports on your computer:

For Each MyPorts As String In _
   My.Computer.Ports.SerialPortNames
   Debug.WriteLine(MyPorts)
Next

On my machine, this puts the following into the Immediate window:

COM1
COM2

If you were surfing when low speed modems were used (or if you still are), you might be aware of the need to initialize your modem by sending it a text string. Here's the code to do that in .NET:

Dim HayesOptima14_4Init As String = _
   "AT &F E0 V0 M1 L0 &C1 &D2 &K3 &S0 S0=1 &W"
Using com1 As IO.Ports.SerialPort = _
My.Computer.Ports.OpenSerialPort("COM1")
   com1.WriteLine(HayesOptima14_4Init)
End Using

It's even easier than using ProComm! (For those who remember those good 'ol days.)

Another use for the My namespace is to access the Windows environment. But the environment is important enough to justify a separate section of this article so let's look at this next.

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Retrieve, Change, or Create Program Information

©2009 About.com, a part of The New York Times Company.

All rights reserved.