Module 2: Development Environment Features
Module 2: Development Environment Features
Environment Features
Contents
Overview 1
Describing the Integrated Development
Environment 2
Creating Visual Basic .NET Projects 3
Demonstration: Creating a
Visual Basic .NET Project 16
Using Development Environment
Features 17
Demonstration: Using the
Visual Studio .NET IDE 29
Debugging Applications 30
Demonstration: Debugging a Project 37
Compiling in Visual Basic .NET 38
Lab 2.1: Exploring the Development
Environment 41
Review 46
Information in this document, including URL and other Internet Web site references, is subject to
change without notice. Unless otherwise noted, the example companies, organizations, products,
domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious,
and no association with any real company, organization, product, domain name, e-mail address,
logo, person, place or event is intended or should be inferred. Complying with all applicable
copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or
for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BizTalk, FrontPage, IntelliSense, JScript,
Microsoft Press, Outlook, PowerPoint, Visio, Visual Basic, Visual C++, Visual C#, Visual
InterDev, Visual J#, Visual SourceSafe, Visual Studio, and Windows Media are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.
Module 2: Development Environment Features 1
Overview
Topic Objective
To provide an overview of
the module topics and
objectives. ! Describing the Integrated Development Environment
Lead-in ! Creating Visual Basic .NET Projects
In this module, you will
learn about the ! Using Development Environment Features
Visual Studio .NET
development environment ! Debugging Applications
and the many powerful
features it provides for ! Compiling in Visual Basic .NET
Visual Basic .NET
developers.
Many aspects of project development in Visual Basic .NET are similar to those
in previous versions of Visual Basic. You still have a range of project templates
to choose from, you still need to reference other projects and applications, and
you still need to set project properties. Visual Basic .NET provides
enhancements to these and other aspects of project development.
In this lesson, you will become familiar with the project templates provided by
Visual Basic .NET. After completing this lesson, you will be able to:
! Choose the correct template for your project.
! Explain how various Visual Basic .NET projects are structured.
! Explain what assemblies are and how to create them.
! Reference other code from your project.
! Create and use namespaces in your projects.
! Use the Imports statement to access objects.
! Set various project properties that affect how your application behaves.
4 Module 2: Development Environment Features
Visual Basic developers are used to having multiple project templates to choose
from when starting a new project. Visual Basic .NET provides many familiar
templates along with a range of new ones.
Template Use this template to create:
Delivery Tip
Point out that students will Windows Application Standard Windows-based applications.
use many of these project
templates during the Class Library Class libraries that provide similar functionality to
remainder of the course. Microsoft ActiveX® dynamic-link libraries (DLLs) by
creating classes accessible to other applications.
Windows Control Library User-defined Windows control projects, which are
similar to ActiveX Control projects in previous versions
of Visual Basic.
ASP.NET Web Application Web applications that will run from an Internet
Information Services (IIS) server and can include Web
pages and XML Web services.
ASP.NET Web Service Web applications that provide XML Web Services to
client applications.
Web Control Library User-defined Web controls that can be reused on Web
pages in the same way that Windows controls can be
reused in Windows applications.
Console Application Console applications that will run from a command line.
Windows Service Windows services that will run continuously regardless
of whether a user is logged on or not. Previous versions
of Visual Basic require you to use third-party products or
low-level application programming interface (API) calls
to create these types of applications.
Other Other templates exist for creating enterprise applications,
deployment projects, and database projects.
Module 2: Development Environment Features 5
Note For more information about Web projects, see Module 7, “Building Web
Applications,” in Course 2373B, Programming with Microsoft
Visual Basic .NET.
Module 2: Development Environment Features 7
Note For more information about assemblies, see Module 10, “Deploying
Applications,” in Course 2373B, Programming with Microsoft
Visual Basic .NET.
8 Module 2: Development Environment Features
You can set project references to other applications or code libraries in order to
use the functionality these applications provide. You can set project references
to other .NET assemblies, existing COM components, or other .NET projects
within the same .NET solution.
! To add a reference
1. Select the current project in Solution Explorer. On the Project menu, click
Add Reference.
2. In the Add Reference dialog box, select the appropriate type of reference
by clicking the .NET, COM, or Projects tab. Only projects within the same
solution are displayed in the Projects tab.
3. Locate the required component in the list, or if item is not displayed in the
list use the Browse button to locate the file. Click the item in the list, and
then click Select.
4. Repeat step 3 for all the components you require, and then click OK.
After you set a reference, you can use it in the same way that you use COM
components in previous versions of Visual Basic. You can view information
about the reference in the Object Browser and create code that uses the
functionality of the reference.
Module 2: Development Environment Features 9
Creating Namespaces
Topic Objective
To explain how to create
namespaces.
Lead-in ! Use Namespace … End Namespace syntax
You can create your own ! Use the root namespace defined in Assembly Properties
namespaces or use the
namespaces that are
defined in the assembly Namespace
Namespace Top
Top 'Fully
'Fully qualified
qualified as
as MyAssembly.Top
MyAssembly.Top
properties. Public
Public Class
Class Inside
Inside 'Fully
'Fully qualified as MyAssembly.Top.Inside
qualified as MyAssembly.Top.Inside
...
...
End
End Class
Class
Namespace
Namespace InsideTop
InsideTop 'Fully
'Fully qualified
qualified as
as MyAssembly.Top.InsideTop
MyAssembly.Top.InsideTop
Public
Public Class
Class Inside
Inside
'Fully
'Fully qualified
qualified as
as MyAssembly.Top.InsideTop.Inside
MyAssembly.Top.InsideTop.Inside
...
...
End
End Class
Class
End
End Namespace
Namespace
End
End Namespace
Namespace
Namespace InsideTop
'Fully qualified as MyAssembly.Top.InsideTop
The following example shows how code from the same assembly, when outside
of the Top namespace, calls classes. Notice that the MyAssembly namespace is
not required as part of the fully qualified name, because this code also resides in
the MyAssembly namespace.
Public Sub Perform( )
Dim x As New Top.Inside( )
Dim y As New Top.InsideTop.Inside( )
...
End Sub
Note You can also create nested namespaces without nesting the definitions,
by using the fully qualified name. For example, you can declare the InsideTop
namespace anywhere by using the following code:
Namespace MyAssembly.Top.InsideTop
12 Module 2: Development Environment Features
Importing Namespaces
Topic Objective
To explain how imports and
aliases can simplify code.
Lead-in ! Fully qualified names can make code hard to read
Referencing the full Dim
Dim xx as
as MyAssembly.Top.InsideTop.Inside
MyAssembly.Top.InsideTop.Inside
namespace makes code
difficult to read. You can ! Using the Imports statement results in simpler code by
avoid this by using the providing scope
Imports statement and
aliases. Imports
Imports MyAssembly.Top.InsideTop
MyAssembly.Top.InsideTop
...
...
Dim
Dim xx as
as Inside
Inside
You can access any object in an assembly by using a fully qualified name. The
problem with this approach is that it makes your code difficult to read, because
variable declarations must include the entire namespace hierarchy for you to
access the desired class or interface.
Module ModMain
Sub Perform( )
Dim x As New Inside( ) 'Fully qualified not needed
...
End Sub
End Module
Import Aliases
You can use the Imports statement to create import aliases for parts of
namespaces. Import aliases provide a convenient way to access items in a
namespace. They prevent naming conflicts but still make code easy to write and
understand.
The following example creates an import alias called IT for the
MyAssembly.Top.InsideTop namespace. You can reference any item
belonging to the namespace by using the IT import alias.
Imports IT = MyAssembly.Top.InsideTop
Module ModMain
Sub Perform( )
Dim x As New IT.Inside( ) 'Alias used
...
End Sub
End Module
14 Module 2: Development Environment Features
You can specify many project properties in the project Property Pages dialog
Delivery Tip box. These properties affect how the project behaves both in the IDE and after it
The project property pages
are shown during the next
is compiled.
demonstration, Creating a The following screen shot shows the project Property Pages dialog box for an
Visual Basic .NET Project.
application named SimpleApp:
Module 2: Development Environment Features 15
Assembly name Specify the name of the assembly when compiled into an .exe
or .dll file.
Root namespace Change the root namespace without affecting the name of the
assembly. (A default root namespace is created when you
create the project.) This property affects any fully qualified
names used for variable declaration.
Project output type Choose what type of assembly is generated when your project
is compiled. You can select Windows Application (.exe),
Console Application (.exe), or Class Library (.dll).
Startup object Select an entry point for your application. This is usually the
main form of your application or a Sub Main procedure.
Class libraries cannot have a startup object.
Importing project- Import multiple namespaces. They are then automatically
level namespaces accessible without forcing you to use the Imports statement in
each file within the project.
Some of the frequently used Configuration Property settings are listed below.
Property Purpose
Debugging settings These properties allow you to set debugging options, like for
previous versions of Visual Basic. You can choose how your
application starts up when debugging by starting the project,
starting an external program that calls your code, or displaying
a Web page from a URL that calls your code. You can also
specify any command-line arguments your application needs
for testing purposes.
Build options You can specify an output directory for your compiled code
(\bin is the default). You can also enable or disable the
generation of debugging information contained in the .pdb file.
16 Module 2: Development Environment Features
In this demonstration, you will learn how to create a Visual Basic .NET project
Delivery Tip based on the project templates. You will also learn about the files that comprise
The step-by-step
instructions for this
the project structure and how to create a reference to another assembly.
demonstration are in the
instructor notes for this
module.
Module 2: Development Environment Features 17
The Visual Studio .NET IDE contains several features that enable more
Delivery Tip efficient development of projects. Some of these features are enhancements of
Several of the IDE windows
will be familiar to
existing Visual Basic features. Others are amalgamated from other sources,
Visual Basic developers, so such as Microsoft Visual InterDev®.
detailed discussion should
After completing this lesson, you will be able to:
not be required.
! Use IDE tools such as Solution Explorer, Server Explorer, Object Browser,
and Task List.
! Use Dynamic Help while developing your Visual Basic .NET applications.
! Edit XML documents in the IDE.
! Record and use macros for repetitive tasks in your projects.
18 Module 2: Development Environment Features
Manipulating Projects
The following features allow you to manipulate your projects with Solution
Explorer:
! Drag-and-drop editing
You can use drag-and-drop editing to move existing project items between
folders.
! Context menus
Most items provide context menus that allow you to perform standard
actions, such as adding items to the project, deleting items from the project,
and excluding items from the project, which removes the file from the
project but does not delete the file. If you use Microsoft
Visual SourceSafe®, you can add items to Visual SourceSafe from
Solution Explorer.
Module 2: Development Environment Features 19
In previous versions of Visual Basic, you can manipulate databases by using the
Data View window. Server Explorer provides the same functionality and
additional functionality for managing and using server components.
Event Logs View system event logs for application, security, and system
events. The Properties window displays information about each
particular event. You can use the context menu to clear the log.
Message Queues Use message queues to send messages asynchronously between
applications. You can view and manipulate any message queues
located on the server by using the context menu for the item.
Performance Use the many performance counters provided by the Windows
Counters platform to monitor system-level and application-level
interactions, such as the total number of logons to the server.
Services Start and stop Windows services from Server Explorer by using
context menus.
SQL Servers View and manage Microsoft SQL Server™ databases directly
from Server Explorer in the same way that you view and
manage data connections.
Method
Class Namespace
Inheritance
Visual Basic .NET enhances the Object Browser found in previous versions of
Visual Basic. Previous versions of the Object Browser show only a high-level
view of objects and their methods. Using the Visual Basic .NET Object
Browser, you can:
! Examine objects and their members within a library, exploring the object
hierarchy to find details about a particular method or item.
! Access lower-level items, such as interfaces and object inheritance details.
! Examine how the .NET Framework class libraries use inheritance in their
object hierarchies.
22 Module 2: Development Environment Features
The following screen shot shows the Microsoft Visual Basic .NET Runtime
library and its various namespaces. This screen shot highlights the
Microsoft.VisualBasic namespace and shows the classes it contains, including
the DateAndTime class, which inherits characteristics from the System.Object
class.
Module 2: Development Environment Features 23
If you use Microsoft Outlook®, you may be familiar with the Tasks feature.
You can use this feature to maintain a list of tasks that you are working on or
tracking, and you can clear tasks when you complete them. Visual Studio .NET
provides the same functionality through a Task List window, which keeps track
of solution-level tasks that you must complete.
Tasks are kept in the .suo project file so that you do not lose information when
you close your Visual Studio .NET session. Any stored tasks are available to all
developers that use the same .suo project files.
Tasks can be added to your Task List in three ways:
! You can manually add tasks to the task list by typing in the top row that is
always visible in the Task List window.
! Visual Studio .NET automatically adds tasks to the list when you attempt to
build your application, when you upgrade from a Visual Basic 6.0 project,
or at various other stages during the project. This allows you to keep track
of what you must do to successfully complete your project.
! You can add tasks by creating comments in your code that use specific
token strings defined in the Options dialog box, which is accessible from
the Tools menu. The TODO, HACK, and UNDONE tokens have been
created for you, but you can define your own.
The following example shows a code section that uses the TODO token and a
custom token named FIX_ASAP:
'TODO create icons for form
'FIX_ASAP bug in form code
24 Module 2: Development Environment Features
The following screen shot shows how the Task List window displays
information based on this example, with three extra items that have been added
to the list manually:
You can use the View menu to specify which types of tasks to display in the
Task List.
The following screen shot shows how to use the Options dialog box to create
the FIX_ASAP token. Notice that the token has been created so that the items
in the Task List display a High priority icon.
Module 2: Development Environment Features 25
The Dynamic Help window automatically displays appropriate Help links to the
Delivery Tip .NET Help files, depending on where the cursor is and what text is highlighted.
Point out to students that
the cursor position is not
As you move from one window to another within the IDE, information
tracked if the Dynamic Help displayed in the Dynamic Help window changes. If you are typing Visual Basic
window is not displayed. syntax, you see the appropriate Help topic for the syntax you are typing.
For example, the results that the Dynamic Help displays for the following
statement vary depending on where the cursor is positioned:
Dim x As Integer
! If the cursor is positioned within the Dim keyword, the Dynamic Help
window displays links relevant to the Dim keyword at the top of the list.
! If the cursor is positioned within the Integer keyword, the Dynamic Help
window displays links relevant to integer types at the top of the list.
26 Module 2: Development Environment Features
You can use the Options dialog box on the Tools menu to configure the items
that the Dynamic Help window displays. The following screen shot shows how
to use the Options dialog box to configure the Dynamic Help window:
Module 2: Development Environment Features 27
Lead-in ! Color-coding
Many of your
! Data View for manipulating data
Visual Basic .NET
applications will use XML
documents to store or
retrieve information. The
IDE provides several XML
features that make it easier
for you to use these types of
documents.
Macros allow users to perform repetitive tasks with the click of a button or
menu item. The Visual Studio .NET IDE provides macros, so you can automate
tasks that require tedious work, such as inserting standard comments into your
code.
The Macro Explorer allows you to edit, rename, delete, or run your macros
within the IDE.
Several sample macros are included in the IDE, including the following:
! Toggle line numbering macros
! Saving or loading Window Views macros
! Debugging macros
You can use any of the sample macros in your projects by executing them in the
Command window or placing them on menus or toolbars.
Any macros you create are stored in a subdirectory of the Visual Studio
Projects folder in My Documents.
Module 2: Development Environment Features 29
In this demonstration, you will learn how to use several features of the
Delivery Tip Visual Studio .NET IDE, including Solution Explorer, Server Explorer, and
The step-by-step
instructions for this
XML editing tools.
demonstration are in the
instructor notes for this
module.
30 Module 2: Development Environment Features
# Debugging Applications
Topic Objective
To introduce the topics
covered in this lesson.
Lead-in ! Setting Breakpoints
The new IDE provides ! Debugging Code
enhanced debugging
features based on those ! Using the Command Window
found in previous versions
of Visual Basic.
The Visual Studio .NET IDE provides enhanced versions of many of the
debugging features found in previous versions of Visual Basic, along with
several powerful features found in Visual C++.
After completing this lesson, you will be able to:
! Set breakpoints.
! Debug code in a Visual Basic .NET project.
! Use the Command window while designing and debugging applications.
Module 2: Development Environment Features 31
Setting Breakpoints
Topic Objective
To explain how to set ! Set breakpoints to halt code execution at a specific line
breakpoints.
! Use the Breakpoint Properties dialog box to
Lead-in set conditions
Setting breakpoints in your
project allows you to step
into your code under a
variety of conditions.
Breakpoints halt execution of code at a specific line. You can set breakpoints at
design time or during a debugging session.
There are several ways you can set a breakpoint:
! Click the margin to the left of the code window on the line containing the
statement where you want the debugger to halt.
! On the Debug menu, click New Breakpoint, and choose from the various
options.
! Place the cursor on the line where you want the debugger to halt. Press F9
to switch the breakpoint on or off.
You can use the Breakpoint Properties dialog box to make a conditional
breakpoint. This feature works in a way similar to watch expressions in
previous versions of Visual Basic. You set a breakpoint condition that only
halts execution when a particular condition is true or when a variable has
changed.
32 Module 2: Development Environment Features
The following screen shot shows a breakpoint condition that only halts when a
variable x has a value of 10.
You may also want to halt execution only when the breakpoint has been
reached and the breakpoint condition has been satisfied a specific number of
times. This number is called the hit count.
The following screen shot shows how you specify that you want execution to
stop the third time that the breakpoint is reached and the breakpoint condition is
satisfied:
Module 2: Development Environment Features 33
Debugging Code
Topic Objective
To discuss how to debug
code and use the various
debugging windows. ! Use the Debug menu or toolbar to step through code
Lead-in ! Use the debugging windows:
Several aspects of
debugging in " Locals: to view and modify local variables
Visual Basic .NET will be
familiar to Visual Basic " Output: to view output from the compiler
developers. " Watch: to view watch expressions
" Call Stack: to view call history, including parameter
information
" Breakpoints: to view, add, or temporarily disable
breakpoints
(continued)
Debug window Use this window to:
Call Stack View the history of calls to the line of code being debugged.
This window displays the history of the call, including any
parameters to procedures and their values.
Breakpoints View a list of current breakpoints, including information such as
how many times the breakpoint has been called, and the conditions
the breakpoint has met.
You can also add new breakpoints and temporarily disable
breakpoints in this window.
Module 2: Development Environment Features 35
In this demonstration, you will learn how to use the debugging features of the
Delivery Tip Visual Studio .NET IDE to debug a simple Visual Basic .NET project.
The step-by-step
instructions for this
demonstration are in the
instructor notes for this
module.
38 Module 2: Development Environment Features
Visual Basic .NET displays compilation errors as you type each statement in
your application code. If you ignore these warnings and attempt to build your
application, the Task List is displayed, with all build errors included on the list.
Information about the error includes the error description, the file in which the
error occurred, and the line number. The error description is the same
information that you see if you position the cursor over the highlighted part of
your code in the code window.
You can edit the errors by double-clicking the appropriate entry in the Task
List. This positions the cursor in the correct file and exact line where the error is
located, so you can make the required modifications. As soon as you complete
your changes and you move off the modified line, the Task List entries are
updated.
40 Module 2: Development Environment Features
Compilation Options
Topic Objective
To describe the options
available when you compile
a project. ! Build configurations
Lead-in " Debug – provides debug information
There are several
compilation options " Release – optimizes code and executable size
available to you when
you compile your ! Build options
Visual Basic .NET projects.
" Build – only builds changed projects
" Rebuild – rebuilds project regardless of changes
The Visual Studio .NET IDE provides several compilation options for building
your Visual Basic .NET projects.
Build Configurations
There are two types of build configurations for Visual Basic .NET projects:
! Debug
During the development phase, you may want to build and test your
applications by using compiled assemblies. The Debug configuration
produces a .pdb file that contains debugging information. Other applications
can use this file to debug your code. To assist these other applications, no
optimizations are made to your code. Other applications have access to your
complete and original code.
! Release
After testing is completed, you will want to deploy your application to client
computers. The Release configuration performs various code optimizations
and attempts to minimize the size of the executable file. No debugging
information is generated for a Release configuration build.
Build Options
You can choose what to build by selecting the appropriate Build menu options.
! Build
The Build option only builds project items whose code has changed since
they were last compiled.
! Rebuild
The Rebuild option compiles all project items even if they have not been
modified since they were last compiled. Use this option when you want to
be sure your application contains the latest code and resources.
Module 2: Development Environment Features 41
Prerequisites
Before working on this lab, you must have experience with developing
applications in an earlier version of Visual Basic.
Scenario
In this lab, you will explore the Visual Studio .NET IDE and use its features to
create a data connection and view event log information. You will create a
simple Windows-based application and add a prewritten form to the project.
Finally, you will debug the application by using the various debugging features
of the IDE.
Exercise 1
Becoming Familiar with the Visual Studio .NET IDE
In this exercise, you will use Server Explorer to create a data connection for the
Northwind SQL Server database. You will investigate Server Explorer, and the
event logs in particular. You will then view the Options dialog box to become
familiar with the default IDE settings.
The purpose of this exercise is for you to become familiar with the IDE, so take
time to explore any parts of the IDE that are interesting to you.
4. Click Test Connection to verify that you have successfully made the
connection, and then click OK.
5. Click OK on the Data Link Properties dialog box.
6. If you are not familiar with the Data View window from previous versions
of Visual Basic or Microsoft Visual InterDev®, explore the list of tables,
views, and stored procedures by expanding the newly created
servername.Northwind.dbo data connection.
Exercise 2
Creating a Visual Basic .NET Project
In this exercise, you will create a simple Visual Basic .NET project and remove
the default form from the project. You will then add a prewritten form to the
project and change the Startup object property of the project.
The prewritten form displays a text box and a command button. When you
press the button, the value in the text box is sent to a subroutine. This
subroutine verifies that the value is not empty and displays a message based on
the value. If the value is empty, an error message appears.
Exercise 3
Using the Debugger
In this exercise, you will use the Visual Studio .NET debugger to debug the
simple application that you created in the previous exercise.
You will set a breakpoint to halt execution in the btnDebug_Click event
handler and use the debugger to step through the subroutine. You will examine
the parameter passed to the PerformValidation procedure and change the
value by using the Locals window. You will then step through the rest of the
code and verify that the correct message appears. Finally, you will modify the
breakpoint so that it is conditional, and use the Command window to perform
various IDE functions.
! To set a breakpoint
1. On the View menu, point to Other Windows, and then click Task List.
2. Right-click anywhere within the Task List window, point to Show Tasks,
and then click All.
3. Double-click the single TODO task to navigate to the comment in the code.
4. Place the pointer on the line immediately after the TODO comment and
press F9, the breakpoint shortcut key.
Review
Topic Objective
To reinforce module
objectives by reviewing key
points. ! Describing the Integrated Development Environment
Lead-in ! Creating Visual Basic .NET Projects
The review questions cover
some of the key concepts ! Using Development Environment Features
taught in the module.
! Debugging Applications
! Compiling in Visual Basic .NET
1. List the file extensions for the following Visual Basic .NET files:
Visual Basic .NET project files, classes, and modules.
.vbproj, .vb, and .vb