Wednesday, December 26, 2007

Visual Basic .NET for Beginners

Finally ... Visual Studio!
We have already seen a few of the tools available in Visual Studio to illustrate OOP earlier. Now, let's develop an example application.
Remember that you can get Visual Studio plus Visual Basic .NET Standard Edition plus a pretty good book for less than a hundred dollars (US) by buying the MSPress Learning Edition. Read all about it
here.
There is one really important feature of VB .NET that may help ease your concerns about deciding that it's time to jump in. If you already use VB 6, you don’t have to abandon your ‘old reliable’ VB 6 environment.
Yep. You heard right. You can install VB.NET right alongside VB 6 and both will run as smoothly together as a ballroom dancing team. A Microsoft web page puts it this way: "It's important to note that .NET can be run side-by-side with previous code. ... This allows for easy testing and the gradual rollout of this technology."
We're going to upgrade our example just slightly for VB .NET by creating a form with two buttons (A and B) rather than just a question on the command line.
Here are the steps to build the 'Two Button Form'. Because this is the most important example, these steps are detailed more carefully than the previous two options. First, the application is built in 'rapid fire steps' so you can have a convenient reference when you do it on your computer. After that, we go over it in more detail to nail down complete understanding!
Start Visual Studio .NET.
On the Start Page, click New Project.
Select Windows Application from the Visual Basic Projects Project Type. You can also change the name of the project and the folder it's stored in on this menu. Click OK.
Drag a Button from the Toolbox to the form and drop it. Drag another one to the form and drop it.
Click Button1 to select it. Open the property window and change the Name property to CommandA. Change the Text property to A.
Select Button2 from the drop down window at the top of the property window and then change the Name property to CommandB. Change the Text property to B.
Double click the first Button object in the form to open the code window for that button.
Enter the code, Msgbox "You clicked Button A" after the automatically entered code Private Sub CommandA_Click( ... (The cursor should already be at the correct place to enter the code.)
Double click the second Button object in the form to open the code window for that button.
Enter the code, Msgbox "You clicked Button B" after the automatically entered code Private Sub CommandB_Click( ...
Click the Run button in the toolbar at the top of the VS.NET development environment window to execute the program.
Aaaaannnnd Now! (drum roll please) ... Click either button A or button B and observe the fruits of your labor!
Now, lets take a more careful look at the VS .NET development environment while we go through building the 'Two Button Form' again.
Steps 1, 2 and 3Start Visual Studio .NET.On the Start Page, click New Project.Select Windows Application from the Visual Basic Projects Project Type. While we're here, let's also change the name of the project to TwoButton and the folder it's stored in on this menu. Click OK.
Steps 4, 5 and 6Drag a Button from the Toolbox to the form and drop it. Drag another one to the form and drop it. Click Button1 to select it. Open the property window and change the Name property to CommandA. Change the Text property to A. Then select Button2 from the drop down window at the top of the property window and then change the Name property to CommandB. Change the Text property to B.
Notice that this is one of the (smaller) differences between VB 6 and VB.NET ... VB.NET changed the same property from Caption to Text. In fact, the component isn't a CommandButton anymore, it's just a Button.
These are pretty small changes, but they signal a huge change in the policy Microsoft used in building VB .NET. It would have been very easy for Microsoft to keep the same name, but instead, Microsoft has changed anything and everything that they thought ought to be different.
This was a big change in attitude! In the steady march from VB1 to VB6, every effort was made to avoid changes so that programmers would be able to migrate to the new version as easily as possible. With VB .NET, however, they recognized at the beginning that they would have to break compatibility completely, so they're using this opportunity to make six versions and ten years of changes all at once. Don't depend on anything being the same unless you know for certain!
Steps 7, 8, 9 and 10
Double click the first Button object in the form to open the code window for that button and enter Msgbox "You clicked Button A" after the automatically entered code Private Sub CommandA_Click( ... . (The cursor should already be at the correct place to enter the code.)Do the same for the second button.
While we're here in the VB .NET editor, check out that box immediately above the event subroutine: Windows Form Designer Generated Code. This is another new facility of VB .NET usually called "Region Code". You can find a more detailed article about it
here. But for now, just click on the small plus to the left and look at (but don't change) the automatically generated code. This is code that you never got to see in VB 6 and having it available now is a double edged sword. While it's possible to change this code, it's seldom a good idea and it's never a good idea unless you know exactly what you're doing. On the other hand, the availability of this code is part of the reason VB .NET is now in the same category of fully object oriented languages like C++. If you do have those rare conditions that require this level of control, it's there to use.
Steps 11 and 12
Click the Run button in the toolbar at the top of the VS .NET development environment window to execute the program. Check out the Debug window that opens automatically at the bottom. In VB 6, you often had to either guess or research endlessly to figure out exactly what the sequence of processing was. VB .NET makes this information a default output. You'll discover as you learn about VB .NET that debug and error information is something that has jumped way ahead.
Whew! If you haven't worked with VB .NET before, this lesson has a lot to chew on! But I can see where you still might be saying, "Big Whoop! A form that tells me that I clicked a button. Gotta write home to Mom about this one!"
Never fear! Next week we'll do something a little more challenging!

No comments: