OOP Tools in Visual Studio 2005


Since OOP is such a fundamental subject in the .NET Framework, there are several tools provided by VS to aid development of OOP applications. In this section, you look at some of these.

The Class View Window

Back in Chapter 2, you saw that the Solution Explorer window shares space with a window called Class View. This window shows you the class hierarchy of your application and enables you to see at a glance the characteristics of the classes you use. For the example project in the previous Try It Out, the view is as shown in Figure 9-3.

image from book
Figure 9-3

This window is divided into two halves, where the bottom half shows members of types. To see this in action with this example project, and to see what else is possible with the Class View window, you need to show some items that are currently hidden. To do this, you need to tick the items in the Class View Grouping drop-down at the top of the Class View window, as shown in Figure 9-4.

image from book
Figure 9-4

Now, you can see members and additional information, as shown in Figure 9-5.

image from book
Figure 9-5

There are many symbols that may be used here, including those shown in the following table.

Icon

Meaning

image from book

Project

image from book

Namespace

image from book

Class

image from book

Interface

image from book

Method

image from book

Property

image from book

Field

image from book

Struct

image from book

Enumeration

image from book

Enumeration item

image from book

Event

Note that some of these are used for type definitions other than classes, such as enumerations and struct types.

Some of the entries may have other symbols placed below them signifying their access level (no symbol appears for public entries). These symbols are shown in the next table.

Icon

Meaning

image from book

Private

image from book

Protected

image from book

Internal

No symbols are used to denote abstract, sealed, or virtual entries.

As well as being able to look at this information here, you can also get access to the relevant code for many of these items. Double-clicking on an item, or right-clicking and selecting Go To Definition, takes you straight to the code in your project that defines the item, if it is available. If the code isn't available, such as code in an inaccessible base type like System.Object, you will instead have the option of selecting Browse Definition, and selecting that will take you to the Object Browser view (which you look at in the next section).

One other section that appears in Figure 9-5 is Project References. This enables you to see what assemblies are referenced by your projects, which in this case includes the core .NET types in mscorlib and system, data access types in system.data, and XML manipulation types in system.xml. The references here can also be expanded, showing you the namespaces and types contained within these assemblies.

There is also a function available from the Class View that enable you to find occurrences of types and members in your code. These are available by right-clicking on an item and selecting Find All References. Either choice gives you a list of search results in the Find Symbol Results window, which appears at the bottom of the screen, as a tabbed window in the Error List display area. You can also rename items using the Class View. If you do this, you're given the option to rename references to the item wherever they occur in your code. This means that you've got no excuse for spelling mistakes in class names because you can change them as often as you like!

The Object Browser

The Object Browser is an expanded version of the Class View window, allowing you to view other classes available to your project, and even completely external classes. It is entered either automatically (for example in the situation noted in the last section) or manually via View Other Windows Object Browser. The view appears in the main window, and you can browse it in the same way as the Class View window.

This window shows the same information as Class View but also shows you more of the .NET types. When an item is selected, you also get information about it in a third window, as shown in Figure 9-6.

image from book
Figure 9-6

In Figure 9-6 the ReadKey(bool intercept) method of the Console class has been selected. (Console is found in the System namespace in the mscorlib assembly.) The information window in the bottom right shows you the method signature, the class the method belongs to, a summary of the method function, and details about the intercept parameter. This information can be very useful when you are exploring the .NET types, or if you are just refreshing your memory about what a particular class can do.

In addition, you can make use of this information window in types that you create. Try making the following change to the code in Ch09Ex01:

 /// <summary> /// This class contains my program! /// </summary> class Program {    static void Main(string[] args)    {       MyComplexClass myObj = new MyComplexClass();       Console.WriteLine(myObj.ToString());       Console.ReadKey();    } }

If you then return to the object browser, you'll see that the change is reflected in the information window. This is an example of XML documentation, which you look at in Chapter 28.

Note

If you made the code change shown above manually, you noticed that simply typing the three slashes /// causes VS to add most of the rest of the typing for you. It automatically analyzes the code to which you are applying XML documentation and builds the basic XML documentation — more evidence, should you need any, that VS is a great tool to work with!

Adding Classes

VS contains tools that can speed up some common tasks, and some of these are applicable to OOP. One of these tools allows you to add new classes to your project with the minimum of typing.

This tool is accessible through the Project Add New Item... menu item or by right-clicking on your project in the Solution Explorer window and selecting the appropriate item. Either way, a dialog appears, allowing you to choose the type of item to add. To add a class, you select the Class entry in the Templates: window, provide a filename for the file that will contain the class, then click Add. The class created will be named according to the filename chosen. (See Figure 9-7.)

image from book
Figure 9-7

In the Try It Out earlier in this chapter, you added class definitions manually to your Program.cs file. It is often the case that keeping classes in separate files makes it easier to keep track of your classes.

Entering the information in the dialog above when the Ch09Ex01 project is open results in the following code being generated in MyNewClass.cs:

 using System; using System.Collections.Generic; using System.Text; namespace Ch09Ex01 { public class MyNewClass { public MyNewClass() { } } } 

This class, MyNewClass, is defined in the same namespace as your entry point class, Program, so you can use it from code just as if it were defined in the same file.

As you can see from the code, the class that is generated for you contains a default constructor.

Class Diagrams

One powerful feature of VS that you haven't looked at yet is the ability to generate class diagrams from code and to use class diagrams to modify projects. The class diagram editor in VS enables you to generate UML-like diagrams of your code with ease. To see this in action, in the following Try It Out you generate a class diagram for the Ch09Ex01 project you created earlier.

Try It Out – Generating a Class Diagram

image from book
  1. Open the Ch09Ex01 project created earlier in this chapter.

  2. In the Solution Explorer window, select Program.cs and click the View Class Diagram button in the Solution Explorer toolbar, as shown in Figure 9-8.

    image from book
    Figure 9-8

  3. A class diagram appears, called ClassDiagram1.cd.

  4. Click on the IMyInterface lollipop and, using the Properties window, change its Position property to Right.

  5. Right-click on MyBase and select Show Base Type from the context menu that appears.

  6. Move the objects in the drawing around by dragging them to give a more pleasing layout. After these steps the diagram should look a little like Figure 9-9.

    image from book
    Figure 9-9

How It Works

With very little effort, you have created a class diagram not unlike the UML diagram presented way back in Figure 9-2. The following features are evident:

  • Classes are shown as blue boxes, including their name and type.

  • Interfaces are shown as green boxes, including their name and type.

  • Inheritance is shown with arrows with white heads and (in some cases) text inside class boxes.

  • Classes implementing interfaces have lollipops.

  • Abstract classes are shown with a dotted outline and italicized name.

  • Sealed classes are shown with a thick black outline.

Clicking on an object shows you additional information in a Class Details window at the bottom of the screen. Here, you can see (and modify) class members. You can also modify class details in the Properties window.

Note

In the next chapter, you take a more in depth look at adding members to classes using the class diagram.

From the Toolbox bar, you can add new items to the diagram, such as classes, interfaces, enums, and so on, and define relationships between objects in the diagram. If you do this, the code for the new items is automatically generated for you.

Using this editor, it is possible to design whole families of types graphically, without ever having to use the code editor. Obviously, when it comes to actually adding the functionality you will have to do things by hand, but this can be a great way to get started.

You come back to this view in later chapters and learn more about what it can do for you, including using the Object Test Bench to test your classes before using them in your code. For now, though, I'll leave you to explore things on your own.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net