Chapter6 PDF
Chapter6 PDF
102
Part II
Upgrading Applications
F06km01
Figure 6-1
F06km02
Figure 6-2
Windows application
Class library
Windows control library
ASP.NET Web application
ASP.NET Web service
Web control library
Console application
Windows service
Empty project
Empty Web project
New project in existing folder
F06km03
103
104
Part II
Upgrading Applications
2.
Drag a Button control from the Toolbox and position it to the right of
the TextBox.
3.
Drag another Button from the Toolbox and position it to the right of
the first button.
4.
Drag a ListBox control to the form and position it below the TextBox
and buttons.
5.
Position the controls so that the form looks roughly like Figure 6-4.
Now lets set some properties for the controls we just added to the form.
1.
105
F06km04
2.
In the property page set the Text property to &Add and the (Name)
property to AddButton.
3.
Click the second button and set the Text property to &Remove and
the Name property to RemoveButton.
4.
Click the ListBox1 control and find the Anchor property. (It is in the
group of properties that specify layout, toward the bottom of the
properties list.) Click the down arrow and make sure the top, left,
right, and bottom sides of the control are selected. Setting these
options will ensure the correct resizing behavior of the control.
You now have a Microsoft .NET application that doesnt do much but look
pretty and resize nicely. Lets add some functionality. Double-click the AddButton control. This takes you to the Code Editor in the AddButton_Click
method. Add the following line of code:
ListBox1.Items.Add(TextBox1.Text)
From the left drop-down menu directly above the editor, select RemoveButton. In the drop-down menu on the right, select the Click event. This creates
a RemoveButton_Click event and positions the cursor inside the event. Now
you should add the following line of code:
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
Not only have we implemented the desired functionality, but weve also
explored two ways of associating event handlers with controls in Visual Basic
.NET. Now that we have the features we want, lets run the application.
106
Part II
Upgrading Applications
F06km05
Figure 6-5
It turns out that everything is not working just fine. Try selecting an item
in the listbox (add one if necessary), and then click the Remove button twice.
You will get the error shown in Figure 6-6.
F06km06
Figure 6-6
So its broken. Click Break and lets dive into the debugger.
107
The Autos window in the lower left side of the IDE displays the value for
SelectedIndex. When no item is selected, the SelectedIndex property is 1. You
can also use the Command window to inspect the SelectedIndex property. Just
type ?ListBox1.SelectedIndex to print out the value in the Command window.
One possible solution is to add code that will check for this condition
before trying to remove an item from the ListBox control. You must end the
debugging session before you can make any changes to the source file. The
current version of Visual Studio .NET locks the source files during debug sessions. Moving to the Code Editor, replace the body of the RemoveButton_Click
event with the following code:
108
Part II
Upgrading Applications
This code prevents the application from attempting to remove any items
from the ListBox unless an item is selected. It will avoid the exception previously encountered at this point. There is, however, another way to do this
possibly a more proper way from a user-interface perspective. It makes sense
that the user should not be able to click the Remove button unless it will actually perform a valid action. To specify this behavior, do the following:
1.
In the Form Designer, select the Remove button and change its
Enabled property to False.
2.
In the Code Editor, select the ListBox1 control from the left dropdown menu. Select the SelectedIndexChanged event from the right
drop-down menu. Add the following code to the new event handler:
If ListBox1.SelectedIndex = -1 Then
RemoveButton.Enabled = False
Else
RemoveButton.Enabled = True
End If
The end result is that by default, the Remove button is disabled at startup.
As soon as an item is selected in the listbox, the Remove button is enabled. If
the user clicks the button, the selected item is removed, and the button is again
disabled.
Miscellaneous Items
The Visual Basic .NET IDE has a whole host of new features. It also introduces
some new behaviors that will take time for Visual Basic developers to get used
to. This section walks you through some of those behaviors.
109
F06km07
Figure 6-7 Your reward for trying to run a project containing errors.
The only purpose of this dialog box is to let you choose between running
a stale build of your solution and fixing the problems and trying again. We cant
really see why youd want to make changes and then run a previous build. The
vast majority of the time you are going to try to fix the problems first. And thats
why you have the Task List.
F06km08
Of course, the Task List has many other purposes. In the Options dialog
box, displayed by choosing Options from the Tools menu, you can select Environment and then select Task List. This dialog box, shown in Figure 6-9, allows
you to customize what is displayed in the Task List and gives you the option
of adding custom tokens. This feature can be a useful way of marking parts
110
Part II
Upgrading Applications
of your application and prioritizing feature areas. For example, you can create tokens that indicate the phase in which features will be added. You could
create tokens like Beta1, Beta2, and Beta3 and filter based on the phase of the
project you are currently in. You could also create tokens like Bill, Bob, or Janice that indicate areas that individuals on your team need to address. This
approach is often useful when implementing large applications.
F06km9
Figure 6-9
Using Breakpoints
Breakpoints work the same way they did in Visual Basic 6. Find a line of code
you want to break into the debugger on, and either click to the far left of the
line in the editing window or right-click the line and select Insert Breakpoint
from the shortcut menu. You can remove breakpoints by clicking the red circle
or right-clicking the line of code and selecting Remove Breakpoint from the
shortcut menu.
111
References
Visual Basic .NET has two types of references that you can add to your project:
a standard reference and a Web reference. A standard reference is used to
import the namespace of either a COM type library or a .NET reference. Web
references are used exclusively for importing a Web service.
Standard References
As Figure 6-10 shows, three kinds of standard references are available to you
through the Add Reference dialog box: COM references, .NET references, and
project references. Use .NET references and project references for referencing
managed components in your project. Adding a COM reference causes the
COM type library to generate a managed type library, thereby enabling the
use of COM objects within your Visual Basic .NET project as if they were
managed classes.
To open the Add Reference dialog box, right-click the References node in
the Solution Explorer, and choose the Add Reference menu item. This opens
the Add Reference dialog box as seen in Figure 6-10.
F06km10
Figure 6-10
112
Part II
Upgrading Applications
Web References
To enable your application to consume Web services, you add a Web reference.
Adding a Web reference generates a local proxy class that enables you to call
methods on the Web service. You add Web references by right-clicking the Reference node in the Solution Explorer and choosing the Add Web Reference menu
item. This opens the Add Web Reference dialog box, as seen in Figure 6-11.
F06km11
Figure 6-11
Problem-Solving Techniques
You need to master a number of skills to become effective at debugging. This
section explores some of those skills.
113
(MSDN) documentation is that the Trace class can be used to instrument release
builds. Instrumentation allows you to monitor the health of your application running in real-life settings, isolate problems, and fix them without disturbing a running system. You can use the Debug class to print debugging information and
check your logic with assertions; you can make your code more robust without
affecting the performance and code size of your shipping product.
The Debugger class is somewhat different. It enables communication
between your application and an attached debugger. This can be useful for inserting code into your application that assists in debugging complex problems or
error scenarios. In addition, some application types (such as Windows services)
can be notoriously difficult when youre trying to trace the root of failures. You
might find these two particular methods of the Debugger object useful:
Using CorDbg
CorDbg is a managed command-line debugger. It can be extremely useful for
handling problems with deployment machines that dont have the Visual Basic
.NET debugger installed. We dont recommend this debugger to the casual
developer, but the simple commands explained in this section will provide a
useful introduction to command-line debugging.
114
Part II
Upgrading Applications
First you need to start the debugger. If you add the path to the directory
containing CorDbg.exe to your environment, you can start the debugger by
typing cordbg at the command prompt. The following sequence of commands is typical:
1.
2.
3.
4.
When an exception occurs, CorDbg breaks to the prompt and reports the
exception type. At this stage you can evaluate where the exception occurred,
get stack trace information, and inspect variable values.
If command-line debuggers arent your style, the Framework SDK also
provides a GUI debugger (DbgCLR, in the %SYSVOL%\Program Files\Microsoft
Visual Studio .NET\FrameworkSDK\GuiDebug directory) that provides a similar debugging experience to the Visual Basic .NET debugger.
If a line like this causes errors and you cant figure out where the error
occurs, try simplifying it by putting each statement on a separate line and storing the results of each statement in a temporary variable. The following sample
shows how to do this:
Dim tempString, formattedString As String, tempDate As Date
tempString = myString & /2002"
tempDate = CDate(tempString)
formattedString = Format(tempDate, d-mmm-yyyy)
MsgBox(formattedString)
115
If an error occurs after youve made the change, its simple to determine
which statement actually caused the error. We are not suggesting you do this
with all your code, but its a useful technique for tracking down the cause of an
error when all you know is that it happens somewhere on a particular line.
Conclusion
This chapter covered a lot of information, from the bare debugging essentials to
more sophisticated troubleshooting methods. We hope this chapter has provided you with a basic understanding of how to work with the IDE and how to
approach debugging applications in Visual Basic .NET. The Visual Basic .NET
debugging experience is significantly different from what youre familiar with in
Visual Basic 6. This helps emphasize the importance of using the techniques
discussed in this chapter to ensure that you can work effectively with your
applications.