Classes are quite simple to build and use, and so is OOP. There are even tools to help you design and build your classes, which I’ll describe briefly here. You can use the Class Diagram Designer to build your classes with point-and-click operations, but you can’t go far on this tool alone. The idea is that you specify the name and the type of a property, and the tool emits the Get and Set procedures for the property (the getters and setters, as they’re known in OOP jargon). The default implementation of setters and getters is trivial, and you’ll have to add your own validation code. You can also create new methods by specifying their names and arguments, but the designer won’t generate any code for you; you must implement the methods yourself. Tools such as the Class Diagram Designer or Visio allow you to visualize the classes that make up a large project and the relations between them, and they’re a necessity in large projects. Many developers, however, build applications of substantial complexity without resorting to tools for automating the process of building classes. You’re welcome to explore these tools, however.
Right-click the name of a class in Solution Explorer and choose View Class Diagram from the context menu. You’ll see a diagram of the class on the design surface, showing all the members of the class. You can add new members, select the type of the properties, and edit existing members. The diagram of a trivial class like the Contact class is also trivial, but the class diagram becomes more helpful as you implement more interrelated classes.
Figure 7.2, in section Inheriting Custom Classes, shows the Product, Book, and Supply classes in the Class Diagram Designer. You can use the commands of each class’s context menu to create new members and edit/remove existing ones. To add a new property, for example, you specify the property’s name and type, and the designer generates the outline of the Set and Get procedures for you. Of course, you must step in and insert your custom validation code in the property’s setter.
To add a new class to the diagram, right-click on the designer’s surface and choose Add Class from the context menu. You’ll be prompted to enter the name of the class and its location: the VB file in which the autogenerated class’s code will be stored. You can specify a new name, or select the file of an existing class and add your new class to it. To create a derived class, you must double-click the box that represents the new class and manually insert the Inherits statement followed by the name of the base class. After you specify the parent class, a line will be added to the diagram joining the two classes. The end of the line at the parent class has an arrow. In other words, the arrow points to the parent class. In addition to classes, you can add other items, including structures, enumerations, and comments. Experiment with the tools of the Class Diagram Designer to jumpstart the process of designing classes. You can also create class diagrams from existing classes. At the very least, you should use this tool to document your classes, especially in a team environment.
To add members to a class, right-click the box that represents the class and choose Add from the context menu. This will lead to a submenu with the members you can add to a class: Method, Property, Field, and Event. You can also add a constructor (although you will have to supply the arguments and the code for parameterized constructors), a destructor, and a constant. To edit a member, such as the type of a property or the arguments of a method, switch to the Class Details window, where you will see the members of the selected class. Expand any member to see its parameters: the type of a property and the arguments and the return value of a method.