Visual studio 2010 part 16 pps

7 120 0
Visual studio 2010 part 16 pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

136 Microsoft Visual Studio 2010: A Beginner’s Guide Navigating a Project with Class View An alternate way to work with projects is via Class view, which allows you to view solutions and project artifacts through the logical layout of the code. In C#, you can open Class view by pressing CTRL-W, C or select Class View from the View menu. In VB you can open Class view by pressing CTRL-SHIFT, C or select View | Other Windows | Class View. Figure 5-12 shows the Class View window. In Class view, you have a hierarchy of nodes that start at the project, include references and namespaces, and contain classes under those namespaces. Under each class you have Base Types, which contains a list of base classes derived from and implemented interfaces for that specific class. Notice how I selected the Student class in Figure 5-12, which shows the members of the class in the bottom pane. As shown in the Class View toolbar, you can create new folders, use the arrows to navigate up or down the hierarchy, or choose options of what to display in the hierarchy. There is also a button with a glyph of objects that indicate how to create a class diagram, which is discussed in the next section. Figure 5-12 The Class View window Chapter 5: Creating and Building Projects 137 Using the Class Designer When working with a project, it can sometimes be helpful to have a high-level view of the project contents, especially if someone else has created the project and you haven’t worked with that project before. This is where the Class Designer can help. In addition to code visualization, another capability of the Class Designer is to give you a basic tool to perform some design yourself. We’ll look at visualizing existing classes first. Class Designer Visualization Whenever you select a project in Solution Explorer, you’ll see the Class Designer button appear in the Solution Explorer toolbar. The Class Designer button also appears on the Class View window. Clicking View Class Diagram will produce a diagram of classes in your solution, shown in Figure 5-13. As you can see in Figure 5-13, VS produces a new file, named ClassDiagram1.cd, with a visual representation of your code. You can see that the properties window is open, allowing you to view information about the selected Program class. Additionally, the Figure 5-13 Visualizing code with the Class Designer 138 Microsoft Visual Studio 2010: A Beginner’s Guide Class Details window provides additional details on the members of the Program.cs class. Figure 5-13 is a minimal diagram of one class with a single method, Main, and you would have seen all of the classes in the current project if there were more. This could be a good way to help you learn about an existing base of code. In addition to code visualization, you have the ability to perform some light design with the Class Designer, as discussed in the next section. Class Designer Code Generation The Class Designer allows you to generate code graphically. On the left-hand side of Figure 5-13, you’ll see a tab for the Toolbox. Hovering over that tab, you’ll see a group of images for code items, such as Class, Enum, Inheritance, and more. Figure 5-14 shows the results of using Toolbox items to enhance the existing Figure 5-14 diagram. In Figure 5-14, you can see the Toolbox with options for what type of items you can add to a class diagram. Each of the Toolbox items matches some type of code that you would normally write. The class diagram itself has additional items, including an abstract Figure 5-14 Generating code with the Class Designer Chapter 5: Creating and Building Projects 139 class named Staff, a normal class named Teacher, an inheritance relationship where Teacher derives from Staff, and an association from Program to Staff. To create a new object, drag-and-drop the object from the Toolbox to the Class Designer surface; you’ll see an input window similar to Figure 5-15. The New Abstract Class window in Figure 5-15 is typical of most of the Class Designer objects you can add to a diagram where you fill in the initial data for naming the class and specifying the file the code will be added to. Not all Toolbox options work this way, though; associations and inheritance work by selecting the item in the Toolbox, selecting the object where the line begins in the Class Designer, and dragging the line to the object in the Class Designer being referenced. The other two places you can modify data are in the Class Details and Properties windows. You can see how I added the GradePapers method in Class Details. You can add members to an object yourself by clicking the object in Class Designer, and then adding the member in Class Details. The GradePapers method also has a Summary comment for documentation and a parameter named papers with a type of List<string>. The Properties window is context-sensitive, showing you what options are available for whatever you have selected in the Class Designer. In Figure 5-14, the Teacher class is selected in Class Designer and the Summary property in the Properties window was filled in with a comment. Listing 5-3 shows the code from the Teacher.cs (Teacher.vb in VB) file that was generated after all of these actions in the graphical designer. Figure 5-15 Adding a new object to the Class Designer 140 Microsoft Visual Studio 2010: A Beginner’s Guide Listing 5-3 Code generated from the Class Designer C#: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ProjectDemo { /// <summary> /// Teaches Classes /// </summary> public class Teacher : Staff { /// <summary> /// Grade student papers /// </summary> /// <param name="papers">Papers to grade</param> public void GradePapers(List<string> papers) { throw new System.NotImplementedException(); } } } VB: ''' <summary> ''' Teaches Classes ''' </summary> Public Class Teacher Inherits Staff ''' <summary> ''' Grade student papers ''' </summary> Public Sub GradePapers(ByVal papers As List(Of String)) End Sub End Class As shown in Listing 5-3, code generated from the Class Designer includes default using directives and the namespace as specified in project properties. The class name, Teacher, Chapter 5: Creating and Building Projects 141 is the same as the visual object in the class diagram, and the GradePapers method is the same as specified in the Class Details window. You can also see the comment on Teacher as specified in the Property window. All that’s left for you to do is replace the call to throw new System.NotImplementedException with your own code in C# or just add your code to GradePapers in VB. Summary You should now know how to create a solution and a project. You can set project properties and add new members to projects. Additionally, you are able to add class libraries to a project and reference those class libraries from other projects that use those libraries. If you prefer a more formal design process, VS offers the Class Designer, which you learned to use for both visualization and code generation. The next chapter builds upon the coding process with VS by showing you how to debug code. This page intentionally left blank . 136 Microsoft Visual Studio 2010: A Beginner’s Guide Navigating a Project with Class View An alternate way to work with. code visualization, another capability of the Class Designer is to give you a basic tool to perform some design yourself. We’ll look at visualizing existing classes first. Class Designer Visualization Whenever. class. Additionally, the Figure 5-13 Visualizing code with the Class Designer 138 Microsoft Visual Studio 2010: A Beginner’s Guide Class Details window provides additional details on the members

Ngày đăng: 04/07/2014, 03:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan