VB.NET_-_Module_3_Language_and_Syntax_Enhancements.pdf

(1170 KB) Pobierz
2373A_03.doc
Module 3: Language
and Syntax
Enhancements
Contents
Overview
1
Data Types
2
Using Variables
9
Demonstration: Using Variables and Data
Structures
20
Functions, Subroutines, and Properties
21
Lab 3.1: Working with Variables and
Procedures
29
Exception Handling
36
Demonstration: Structured Exception
Handling
48
Lab 3.2: Implementing Structured
Exception Handling
49
Review
52
This course is based on the prerelease version (Beta 2) of Microsoft® Visual Studio® .NET
Enterprise Edition. Content in the final release of the course may be different from the
content included in this prerelease version. All labs in the course are to be completed with
the Beta 2 version of Visual Studio .NET Enterprise Edition.
72016393.004.png 72016393.005.png 72016393.006.png
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.
© 2001 Microsoft Corporation. All rights reserved.
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 Studio, and Windows Media are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their
respective owners.
72016393.007.png
Module 3: Language and Syntax Enhancements
iii
Instructor Notes
Presentation:
90 Minutes
This module provides students with the knowledge needed to use many of the
new language and syntax enhancements to the Microsoft ® Visual Basic ®
language, including variable declaration and initialization, procedure syntax,
structured exception handling, and assignment operator changes.
Labs:
75 Minutes
In the labs, students will create a simple application that uses variables and
procedures to add customers to a customer array. The students will then add
structured exception handling to the application.
After completing this module, students will be able to:
¡ Describe the changes to data types in Visual Basic .NET.
¡ Declare and initialize variables and arrays.
¡ Use shorthand syntax to assign values to variables.
¡ Implement functions and subroutines.
¡ Call the default properties of an object.
¡ Use the new Try…Catch…Finally statement to implement structured
exception handling.
Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
Required Materials
To teach this module, you need the following materials:
¡ Microsoft PowerPoint ® file 2373A_03.ppt
¡ Module 3, “Language and Syntax Enhancements”
¡ Lab 3.1, Working with Variables and Procedures
¡ Lab 3.2, Implementing Structured Exception Handling
Preparation Tasks
To prepare for this module, you should:
¡ Read all of the materials for this module.
¡ Read the instructor notes and the margin notes for the module.
¡ Practice the demonstrations.
¡ Complete the labs.
72016393.001.png
iv
Module 3: Language and Syntax Enhancements
Demonstrations
This section provides demonstration procedures that will not fit in the margin
notes or are not appropriate for the student notes.
Using Variables and Data Structures
è To test the application
1. Open the DataTypes.sln file in the install folder \DemoCode\
Mod03\DataTypes folder.
2. In the code for frmData.vb, set breakpoints on the first lines of the
TestVariables and TestStructures subroutines.
3. Run the project.
è To debug the Variables button
1. Click the Variables button on the form. The Visual Basic process will halt
at the first breakpoint.
2. Explain the code, pointing out that both intA and intB are created as Integer
data types.
3. Step through the code by using F11, and explain the new assignment
operators and the CType function.
4. Allow the program to continue by pressing F5.
5. Stop the project and uncomment the Option Strict code at the top of the
form code. Attempt to run the project. A compilation error will occur.
Explain why this occurs. Re-comment the Option Strict code, and run the
project to test the Structures code.
è To debug the Structures button
1. Click the Structures button on the form, and step through the
TestStructures subroutine. Point out the Employee structure definition at
the top of the form code before continuing to debug. Note that the array is
initialized to a size of two employees. Continue debugging the code by
using F11. Explain each line when required.
2. Point out the block-level variable iCounter . Allow the remaining code to
execute before closing the form and stopping the debugger.
è To create an out-of-scope variable exception
1. Return to the form code and uncomment the final line in the TestStructures
routine MsgBox( iCounter ) . Attempt to run the project again, and observe
the error message that is generated because of the attempt to access the
block-level variable outside of its scope.
2. Close the Microsoft Visual Studio ® .NET integrated development
environment (IDE).
72016393.002.png
Module 3: Language and Syntax Enhancements
v
Structured Exception Handling
è To test the application
1. Open the Exceptions.sln solution in the install folder \DemoCode\
Mod03\Exceptions folder.
2. Open the code window for the Errors.vb form and set breakpoints on the
Try statement, each Catch statement, and the Finally statement in the
RunExceptions routine.
3. Run the Exceptions project.
4. Click the Overflow button on the form. The Visual Basic process will halt
at the first breakpoint.
5. Use F11 to step through the code in the Try block, and explain the overflow
details. Explain that the OverflowException class is filtering the exception.
Step through the remaining code, and allow execution to continue.
6. Click the remaining buttons on the test form ( Divide , Err.Raise , and
Throw ) to demonstrate how the exceptions are handled in each case.
7. End the debugging session by closing the form.
8. Close the Visual Studio .NET IDE.
72016393.003.png
Zgłoś jeśli naruszono regulamin