Download chapter 5: 'Building the business logic layer'
Excerpted from the book "Doing Objects in Visual Basic 2005," ISBN 0-321-32049-2, Copyright 2007. Written permission from Addison-Wesley is required for all other uses. Copyright © 2006 Addison-Wesley. All rights reserved.
Chapter Excerpt:
Adding a Class to a Project
There are many ways to add a class to a project. As discussed in the preceding chapter, adding a form project item actually adds two class files to the project. It adds a class in one file with a .vb extension and a partial class in another file with a .designer.vb extension.
When building the business logic layer, you normally add one
class project item for each business object class (such as Product and
Customer) and one for each implementation class (such as Logging and
Security).
-
To add a class to a project:
-
- Right-click the project in Solution Explorer and select Add | New Item from the context menu, or select Project | Add New Item from the main menu bar. Alternatively, you could select Add | Class from the context menu, or select Project | Add Class from the main menu bar.
- Select the Class template, name the class, and click the Add button.
If you created your own class template using the steps in Chapter 3, you can use your template here.Use standard naming conventions for your class name. The most common standard is to name the class using the singular name of the business entity or implementation feature represented by the class. For products the class name would be
Product, for logging the class name would beLogging, and so on.Visual Studio creates the class file with a .vb extension, adds it to Solution Explorer, and then displays the class in the Code Editor.
When Visual Studio creates the class file, it automatically generates the class declaration as follows:
Public Class Product End ClassYou can add any number of classes to your projects as needed by your application. Regardless of the class's purpose or location, the basic process of building a class is the same.
-
To document the class:
- Open the class in the Code Editor.
- Move the insertion point immediately before the word
Publicin thePublic Classstatement. - Type three comment markers, defined in Visual Basic as apostrophes
('''), and press the Enter key. The XML comments feature automatically creates the structure of your class documentation as follows:''' <summary> ''' ''' </summary> ''' <remarks></remarks> Public Class Product End Class
Documenting the Class
It is always a good idea to add documentation for a class immediately after adding the class. By adding the documentation right away, you focus on the class's purpose, which helps you keep the class encapsulated. It is also much easier to document each class as you go along instead of facing the large task of going back later and documenting all the classes.
-
4. Type a summary of the class's purpose between the summary tags
and any remarks between the remark tags.
Your documentation may be similar to this:
''' <summary> ''' Provides product management features such as ''' retrieving product data and saving product changes ''' </summary> ''' <remarks>Use this class to work with products ''' </remarks>
Use the summary tags to describe the class and the remarks tags to
add supplemental information. The summary is the most important tag
because it is the one used by Visual Studio.
When you provide a summary of the class using XML comments,
your class displays documentation about itself in appropriate places within
Visual Studio, such as in the List Members box, shown in Figure 5.1. Open
the List Members box by typing a part of the class name in the Code
Editor and pressing Ctrl+Spacebar or by selecting Edit | Intellisense |
List Members from the main menu bar or by clicking the Display an
Object Member List icon on the Text Editor toolbar.
Using XML comments to document your classes makes it easier for you and other developers to work with your classes.
Chapter 5: 'Building the business logic layer'
Visit the Addison-Wesley website for a detailed description and to learn how to purchase this title.
This was first published in March 2007
Join the conversationComment
Share
Comments
Results
Contribute to the conversation