What's in Visual Basic 2005 for the Visual Basic 6.0 Developer
Microsoft Visual Basic 2005
Microsoft Visual Basic 6.0
Summary: Review the exciting new changes in version 2.0 of the Microsoft Visual Basic .NET framework that make the switch for Visual Basic 6.0 developers an easy and functional move. (18 printed pages)
I Haven't Needed .NET for Anything so Far...
I'll Take Some of the Blame
Giving Credit Where Credit Is Due
Gone with the Grid
I Want That Toolbar
You Know What I Mean
Try This, It's Free
What Part of "I Don't Want to Install Visual Studio 2005" Don't You Understand?
The Microsoft Hype Machine has already started turning up the volume on Visual Studio 2005. The question is, if you work primarily in Visual Basic 6, you're happy with Visual Basic 6, and you've looked at .NET and not found anything you really need, should you even care about this new release of Visual Studio 2005? Considering that MSDN has been nice enough to host this article, you can probably guess the conclusion that I've reached. But bear with me, there are some great things coming for the Visual Basic 6 developer.
First of all, let's just admit that for many of us, Visual Basic .NET left something to be desired. If you think back to when Microsoft unleashed .NET on the world, the megaphone was blaring only two things: Web Service and C#. Well, many of us Visual Basic 6 developers, didn't need (or even want) Web Services. And, we weren't really interested in learning this new C# language when we already had a good language. After Web Services and C#, the other thing that Microsoft was blaring was ASP.NET. While many of us do some Web development, lots of Visual Basic 6 developers are primarily interested in desktop applications, so if .NET was all about Web Services, C#, and ASP.NET, then Microsoft should understand if many of us did not find anything in .NET that we couldn't live without.
Finally, let's just say that Visual Basic .NET wasn't 100 percent compatible with Visual Basic 6. This made it non-trivial to move a Visual Basic 6 application to .NET, assuming that I wanted to.
Like most Visual Basic developers, there were some things that I wanted for my language. I wanted the glass ceiling to be gone. I was tired of "you can't do X in Visual Basic 6." I wanted my language to be able to do anything. I also wanted the language to support at least some simple object oriented concepts like inheritance. Be careful what you ask for...
Visual Basic .NET provides 100 percent access to the full .NET framework. There is no glass ceiling. You can do anything in .NET from Visual Basic .NET. It's also fully object oriented, to a fault. Instead of something simple like "Form1.Show" working, you have to treat everything like the class that it really is. Meaning that to show a form, you have to do the following:
Listing 1. Showing a form with Visual Basic .NET
Dim f as New Form1() f.Show()
In addition, some of my other favorite features were apparently just too hard to do in the architecture of .NET v1.0. Edit and Continue, for example, didn't make the cut.
I have to give Microsoft credit for one thing; they do listen. Since the release of Visual Basic .NET, Microsoft has been working very hard to bring back a lot of the favorite features of Visual Basic 6, without dumbing down the language. As a result, in Visual Studio 2005, "Form1.Show" works again.
Figure 1. Displaying forms, the Visual Basic 6 way
Edit and Continue is back as well. In fact, it's better than before. With Visual Basic 6, Edit and Continue worked almost too well. You could F5 for a long time, working on your application, just to find out that when you try to make the executable, it doesn't compile because there's an error someplace. With Visual Basic .NET, every time you hit F5, it makes sure that all the code will compile, but still gives you the full ability to change your code and continue execution.
Figure 2. Edit and Continue
Visual Studio 2005 doesn't just reintroduce lost Visual Basic 6 features. In many cases, it takes these features to a new level. First, think about designing a form in Visual Basic 6. When you design a form, there are a number of things that have always bothered me. First, if you just double-click a control in the toolbox, it places it on the form using some default size, but it's not the size you would ever want. Second, when you line up controls, you're doing it based on the grid dots. This makes it somewhat simple to align the top or left side of controls, but harder to align the bottom or right edges. Also, if you actually want the text inside of the controls to be on the same line, good luck.
Figure 3. Designing a form in Visual Basic 6
When I first looked at the Visual Studio 2005 form designer, my first thought was "Where's the grid? How can you possibly design a form without a grid?" You'll find the form designer in Visual Studio 2005 to be nothing short of awesome. What I realized is that you really want to align controls based on other controls, or the edges of the forms, and you want to be able to easily lay out controls with some standard spacing between them. Visual Studio 2005 does this with "snap lines." Visual Studio 2005 also defaults controls to correct sizes, and does lots of other little things to make it very productive to build forms.
Figure 4. Snaplines to align controls
Another common complaint about Visual Basic 6 was some of the controls it included. For example, the menus and toolbars were really limited compared to the menus and toolbars that Microsoft used in their own applications.
Figure 5. The office toolbar
Take the office toolbar, for example. This toolbar not only has buttons, but it can host text boxes, drop-downs, and separator controls. In addition, you can drag the toolbars around and stack them however you want. The menu bar can also contain text boxes and other controls. The chorus from the Visual Basic 6 camp has long been "Why can't you give us the same controls that you use?"
The answer from Visual Studio 2005: "Done."
Figure 6. Office look and feel from Visual Studio 2005
Simple mistakes are easy to make when coding. Visual Studio 2005 is able to figure out these common errors, give you a meaningful error message, and say "Would you like me to fix this for you?"
Figure 7. Autocorrect in Visual Studio 2005
If it can't be autocorrected as you type it, or caught at compile time, the next best thing is a good help when an error happens at runtime. With Visual Basic 6, if you forget to "New" an object you get the error "Object variable or with block not set." With Visual Studio 2005, you get an error that actually lets you know, "Hey, you forgot to use New."
Figure 8. The Exception Assistant
What I want from a development environment isn't a bunch of wizards. To use the analogy of a car, I don't want something that will drive the car for me, because (1) it will probably get me in a wreck, and (2) I wouldn't trust it to take the right route. However, I do love power steering, power brakes, cruise control, and so on. These just help take the grunt work out of driving. I'm still fully in control of every move the car makes, but I don't have to expend as much effort to wrestle the car down the road.
I like things that give me the same experience for the development environment. I want to stay fully in control of what code gets written, but I want the IDE to take a lot of the grunt work out of writing that code.
Visual Basic 6 made a simple start down that path with the Add Procedure tool. I use this tool quite a bit for properties, because it writes a lot of the boilerplate for me.
Figure 9. Add procedure tool
With Visual Studio 2005, you get this on steroids. First, if you just type in "Public Property UserName as String," the IDE will splat in all the boilerplate, giving you:
Listing 2. Visual Studio auto-generating a property procedure
Public Property UserName as String Get End Get Set End Set End Property
And with the addition of the free Refactor! add-in, the IDE seems like it can read your mind.
Figure 10. Using Refactor! to create a property procedure
The Refactor! add-in can automate a number of common tasks. For example, if you want to turn a section of code into its own separate function, Refactor! will let you extract it, and will even figure out what arguments need to be passed in. You can easily convert hard-coded numbers or strings into constants. You can also highlight some expression, and convert it to a local variable (I find myself doing this all the time).
Microsoft's making another smart move with Visual Studio 2005. They're releasing free versions for Visual Basic .NET and Web development (as well as C# and C++). They're also releasing a replacement for MSDE that is actually easy to deploy with your applications. The light-weight, free, Visual Basic development environment is called Visual Basic Express. This gives you a good opportunity to experiment with .NET without making any expensive investments.
Figure 11. Visual Basic Express
Visual Basic Express contains the common functionality that you need to build stand-alone applications, or class libraries. You get the same toolbox, designer, base classes, code editor, and so on, that you get with the full Visual Studio product.
I mentioned earlier that things like "Form1.Show" work again. In Visual Basic parlance, this is referred to as a "default instance," meaning that by default, there's a Form1 created that you can start working with, and you don't have to New one up from scratch. There are, however, a lot of other default instances now at your disposal as well, and they're available off of the "My" keyword.
Table 1 My namespace commands
|My.Computer.Network.IsConnected||Determines if the computer has a network connection, or is working disconnected.|
|My.Computer.Info.TotalPhysicalMemory||The amount of memory installed.|
|My.Computer.Audio.Play||Play an audio file.|
|My.Computer.FileSystem.Drives||List of logical disk drives.|
|My.Computer.FileSystem.FindInFiles||Searches files in a folder and returns a list of files that contain a certain string.|
|My.Computer.Network.Ping||Returns true or false depending on whether a network address responds to a ping.|
|My.Computer.Network.DownloadFile||Download a file to disk.|
|My.Application.SplashScreen||Gets or sets the splash screen for the application.|
|My.Application.Info.*||Company name, product name, copyright, and other developer specified application information.|
|My.Settings.*||Gets, sets, saves, and reloads custom application settings.|
Figure 12. Using the My Namespace
Even if you never plan on installing Visual Studio 2005, there's a ton of stuff in version 2.0 of the .NET framework that you can use from your Visual Basic 6 applications. The whole point of Visual Basic Fusion is that you can use everything in .NET from Visual Basic 6, without any rewriting of your Visual Basic 6 code. This is just as true with version 2.0 of the framework as it is with version 1.1. You can just create a simple wrapper class around the .NET functionality that exposes it as a COM object. From there, you can use this functionality from Visual Basic 6, VBA, ASP, or VBScript.
Listing 3. Using Visual Basic .NET to expose .NET functionality as a COM object
<ComClass(MyWrapper.ClassId, MyWrapper.InterfaceId, MyWrapper.EventsId)> _ Public Class MyWrapper #Region "COM GUIDs" ' These GUIDs provide the COM identity for this class ' and its COM interfaces. If you change them, existing ' clients will no longer be able to access the class. Public Const ClassId As String = "36d8911d-0395-4961-9893-4325fcb5e522" Public Const InterfaceId As String = "ae2c5486-fbc4-4043-83f2-1687161858c9" Public Const EventsId As String = "0e5e9245-e331-41e8-b84c-6ad3dc69d9bd" #End Region ' A creatable COM class must have a Public Sub New() ' with no parameters, otherwise, the class will not be ' registered in the COM registry and cannot be created ' via CreateObject. Public Sub New() MyBase.New() End Sub Public Function NetworkIsAvailable() As Boolean Return My.Computer.Network.IsAvailable() End Function End Class
This creates a COM object that you can easily use from Visual Basic 6 or other COM based environments.
Listing 4. Determining network availability from Visual Basic 6
Dim c as NetFrameworkWrapper.MyWrapper Set c = new NetFrameworkWrapper.MyWrapper If c.NetworkIsAvailable Then ... End If
I think version 2.0 of the .NET framework is a great gift to the Visual Basic 6 developer. You can redistribute the framework completely free of charge, and with Visual Basic Express, you have a free development environment that you can use to build libraries or whole applications. This means that you can extend your Visual Basic 6 applications with the .NET Framework without spending a dime. And, version 2.0 of the .NET framework provides some really great functionality that just wasn't available in Visual Basic 6. In addition, Visual Basic .NET has been fixed to be a lot more like Visual Basic 6, but without dumbing down the language in any way. The development environment lets you layout forms very quickly, and the IDE saves you umpteen keystrokes as it injects boilerplate code for you, and allows you to quickly rework existing code. There's also great help for coding and runtime errors with autocorrect and the Exception Assistant. All-in-all, I think that this is a strong addition for the Visual Basic 6 developer. If you want to continue to use Visual Basic 6, you have now been given thousands of new classes in version 2.0 of the framework that you can use from your existing Visual Basic 6 applications. If you like what you see in the development environment, it's free to use with Visual Basic Express.
About the author
Scott Swigart (www.swigartconsulting.com) spends his time consulting with companies on how to best use today's technology and prepare for tomorrows. Along this theme, Scott is a proud contributor to the VB Fusion site, as it offers information and tactics of real use for Visual Basic developers who want to build the most functionality with the least effort. Scott is also a Microsoft MVP, and co-author of numerous books and articles. Scott can be reached at email@example.com.