The BASIC programming language was an extremely simple and easy tool. Visual Basic integrated that easy language into a Visual IDE, and it was a in relatively easy language to learn work. But it could not do a lot of things that more powerful languages like C++ could do. Programmers wanted more functionality. As Visual Basic progressed from version to version new features were added. But it still fell short . So starting with.NET Visual Basic is different from its predecessors. Its built on the .Net Framework and accesses the Common Language Runtime classes. Visual Basic 2010 is a complex and powerful language, and that means that it is much more complicated than Vb6.
Having programmed at professional levels I can testify that every programmer that I’ve met routinely scans the object browser, and other help systems to find available properties, objects, and the syntax required to program. They also become expert at debugging
Help is provided at a number of levels. In the properties window we see the explanation of the property at the bottom of the window. IntelliSense is it is in itself a help system as it enables you to see all of the controls with their available properties and methods when you are programming. Tooltips pop up over objects in your code and in other windows to give definitions, and you can right-click over objects in your code and click on Go To Definition to go to the object browser which displays the catalog of objects available for coding.
You can also press F1 to go to the help associated with the program. Depending on your setup it will either open a help file on your computer or it will open a webpage with help from the MSDN web.
In the above image you’ll see that below the Object Browser is the Error List. As you’re typing code, and after you attempt to build the program the Error List will display errors that you have in your program. These tools will all help you avoid what is known as “build” errors, errors in your program that can be seen when you build the program. Compilers have built in checks so that when you try to build a program the compiler will find as many errors as it can, especially syntax errors and typos, incorrect properties and methods, and incorrect procedure calls. These are all types of build errors
Another type of error is what is known as a “runtime” error. A runtime error, usually some form of logic error, is an error that can only be seen after you run the program. Finding runtime errors can be more problematic and time-consuming, but fortunately, the Visual Basic IDE has numerous tools to speed the process along.
Debugging A Running Program
Open the LearningVB1 project, and open the code window for Form1.vb (Select Form1.vb in the Solution Explore, and click the View Code button in the Solution Explorer window toolbar.)
We originally programmed the form without any error handling or trapping. In the last couple of lessons we we have programmed several ways to avoid the divide by zero error.
Restore the code to look the the original:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dNumber1 As Decimal
Dim dNumber2 As Decimal
Dim dNumberResult As Decimal
dNumber1 = CDec(TextBox1.Text)
dNumber2 = CDec(TextBox2.Text)
If RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2
If RadioButton2.Checked = True Then dNumberResult = dNumber1 – dNumber2
If RadioButton3.Checked = True Then dNumberResult = dNumber1 * dNumber2
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
TextBox3.Text = CStr(dNumberResult)
End Sub
Now, run the program and set it up to divide by zero:
Click on that Calculate button, and it may take a minute, but eventually your program should stop running, and your code window should be open with an error message:
We just tried to do something illegal, divide by zero. Everybody knows you can’t divide by zero! It’s a little later we will write a “error handler” to take care this. But first, Ruzicka quick look at some of the other debugging tools available in the Visual Basic IDE.
For now, just stop the program by clicking on the “Stop Debugging” button on the toolbar
Stepping Through The Program
A very handy tool provided by the Visual Basic IDE is the ability to step through the program line by line. As the program processes each line you can move the mouse over the different objects in the line of code, and get the current value of variables and properties.
To step through the whole program, click Debug, then Step Into in the menubar. Press F8 to step through the program line by line. In the next image I have stepped until I am in this sub procedure where the Calculate button is clicked and I am on the line where the first variable is assigned the value of the first text box converted to decimal. Notice that the value at that point is zero:
Next press F8 and the program will move down the line. Now you should see the value of the first variable,dNumber1, is 5125, or whatever number you type in text box 1.
Keep pressing F8 to run through the program, and notice how the variable values change, and how the program executes on the If statements.
Stop debugging the program
Assigning Break Points
Stepping through the whole program is simple for a small program like we have so far, but for most programs, is very unrealistic. Most programs are built one section at a time, and debug one section of the time. For that reason, handy facility is the ability to set breakpoints at different points in the program.
You set a breakpoint by clicking in the left margin by a line of executable code:
Now we can run the program, and it will run until the line(s) with a breakpoint are reached. Now you can use the F8 and Step Over (Shift + F8) functions to go through your program line by line.
The Immediate Window
All of the debugging Windows are available under Debug in the menubar.
The Immediate window is used at design time for debugging and evaluating expressions. You can even execute statements in it.
The Immediate Window is only available during run time. To access the Immediate Window if it is not present, press Ctrl+G.
Example:
Run the program again. In the Immediate Window, type: ? (dNumber1 + 100) and press Enter. If it is before the variable has been assigned a value then you should get a result of 100D where D represents Decimal. If it is after it has been assigned a value you should get the new value plus 100.
The Locals Window
Again, our program is very simple and easy to follow. But in larger programs there are many variables, and properties. Often you will need to know the values of numerous properties and variables, and you can do that using the Locals Window.
Run the program again and this time open the Locals window, and watch the variables change value as you step to the program.
Writing An Error Handler
For those of you who have used Visual Basic 6, Visual Basic.Net supports unstructured error handling. That means that you will find programs, and you yourself can still use the On Error statement to trap errors. We will look at the Try… Catch… Finally structure that.net provides in our example
Now we are going to add the Try… Catch… Finally structure so the code looks like the following. Note that we start our “trapping” by placing the Try statement before the first line of code that uses any kind of logic to perform any kind of operation. And we put the Catch, Finally, and End Try statements after the last line of code that uses any kind of logic to perform any kind of operation. In our little section of code these lines start with dNumber1 = CDec(TextBox1.Text) because this line is more than just assigning a value to a variable, it is using the CDec function to convert the value, and thus is performing an operation. However, just assigning a variable the value in a text box can cause an error if the value in the text box is of a different “type” then the type assigned to the variable.
Add the Try…Catch…Finally code into the program as seen below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dNumber1 As Decimal
Dim dNumber2 As Decimal
Dim dNumberResult As Decimal
Try
dNumber1 = CDec(TextBox1.Text)
dNumber2 = CDec(TextBox2.Text)
If RadioButton1.Checked = True Then dNumberResult = dNumber1 + dNumber2
If RadioButton2.Checked = True Then dNumberResult = dNumber1 – dNumber2
If RadioButton3.Checked = True Then dNumberResult = dNumber1 * dNumber2
If RadioButton4.Checked = True Then dNumberResult = dNumber1 / dNumber2
Catch ex As Exception
[we will insert the Messagebox.show statement below]
Finally
TextBox3.Text = CStr(dNumberResult)
End Try
End Sub
In the above example we put in the “Finally” section the code that puts the converted value of dNumberResult into TextBox3. In this example finally means that no matter what happens, that’s the value that’s going to go to the text box.
Next we are going to put code in the Catch section to tell the program what to do when it finds an error. If you have been using computers for any time at all you’ll be familiar with pop-up boxes that say that an error has occurred. Users expect this so we will use the “exception” that the system gives when an error occurs and pass this information along to the user in a message box. The code for that looks like this:
MessageBox.Show(“The computer experienced an error when trying to perform an operation: ” & ex.Message, “Error”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Now, save all files, build the program, and run it. Next, change the program to try to divide by zero:
When you click Calculate, you should get an error message like this:
We have just “handled” an error. The program did not crash, but rather it informed the user that the user had performed an invalid operation, and the program continued to run.
This exercise demonstrates the “exception” that is common to all the languages in the.net framework. There are actually a number of exception classes in the.net framework. The one a we accessed is actually a generic System exception, but there are more specific exceptions like FileNotFound xception, and IndexOutOfRangeException.
Other Debugging Tools
There are more tools then we discussed in this introductory article. There are settings in project properties under the debug tab. There you can enter command line commands, or enable unmanaged code debugging, topics beyond the scope of an introductory class. We also didn’t discuss the Breakpoints Window, the Command Window, the Call Stack Window, or the Trace class which is a class exposed by the System.Diagnostics namespace. Finally another option that is sometimes used is to write output to application logs.
Old Time Methods Revisited
Occasionally there will be a bug that will avoid you using the above methods. Or, perhaps you want to see what is on your form as you are running your program. A throwback to older versions is the use of the MessageBox.
We use the MessageBox in our Try structure. Another use of the MessageBox is to put results of code in the MessageBox and have them pop up at regular intervals in your code. For example, the Visual Basic IDE will not debug SQL statements that you pass to SQL Server. Now you can put the SQL statement into a variable which you can see in the Immediate Window as discussed above, but you can also just display it a MessageBox before it’s sent to the server, and sometimes that is enough for you to you to see your error.