Debugging an Application

To debug a Visual Basic application, you basically need to perform two steps:

1. Enable the Debug configuration in the compile options.

2. Press F5 to start debugging. Visual Studio runs your application and attaches an instance of the debugger to the application.

Because the Visual Studio debugger needs the debug symbols in order to proceed, if you do not choose the Debug configuration, you cannot debug your applications. The instance of the debugger detaches when you shut down your application.


Tip

As an alternative, you can click the Start button on the Visual Studio standard toolbar. If the Debug configuration is selected, this action does the same thing as pressing F5. If the Release configuration is selected, selecting Start does the same thing as launching the application with Ctrl+F5.


The debugger monitors your application’s execution and notifies you of runtime errors; it allows you to take control over the execution flow as well. Figure 2.30 shows our sample application running with the Visual Studio debugger attached.

Image

FIGURE 2.30 Our sample application running with an attached instance of the Visual Studio debugger.

In the bottom area of the IDE, notice that some tabs are available, such as Locals, Watch 1, Watch 2, Call Stack, Breakpoints, Command Window, Immediate Window, and Output. Each tab represents a tool window that has specific debugging purposes. Also, notice that the status bar becomes orange, and an orange border is placed around the IDE, to remind you that the IDE is running in debugging mode.

The Visual Studio debugger is a powerful tool. Next, you’ll learn the most important tasks in debugging applications. Before you learn about the tools, though, you need to modify the source code of our test application so that it can intentionally cause some errors and you can see the debugger in action. The Sub Main method’s code could be rewritten as shown in Listing 2.3.

LISTING 2.3 Modifying Sub Main for Debugging Purposes


Sub Main()
    'A text message
    Dim message As String = "Hello Visual Basic 2015!"
    Console.WriteLine(message)
    'Attempt to read a file that does not exist
    Dim getSomeText As String =
                    My.Computer.FileSystem.ReadAllText("FakeFile.txt")
    Console.WriteLine(getSomeText)
    Console.ReadLine()
End Sub



New to Visual Basic .Net?

If you are not an existing Visual Basic .NET developer, you may not know some of the objects and keywords shown in the code listings in this chapter. We’ve kept the code simple so that it’s easy to understand; and we’ve also provided comments. The next chapters guide you through the details of the programming language, and everything used here will be explained. At the moment, you should focus on the instrumentation more than on the code.


This code simply declares a message object of type String, which contains a text message. This message is then shown in the Console window. This is useful for understanding breakpoints and other features in the code editor. The second part of the code tries to open a text file, which effectively does not exist, and store its content in a variable called getSomeText of type String. This will help you understand how the debugger catches errors at runtime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.135.80