![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Designing The ClassTo illustrate the basic concepts of classes, and to demonstrate how to use Visual Basics Class Builder utility, we will work through the process of creating a class, along with a demonstration project to show you how it works. Well start off with something simple, so the details do not get in the way of learning the underlying concepts. We will create a class that stores string data and improves on Visual Basics own string data type by providing the ability to determine the length of the string and also to insert characters at a specified location within the string. Lets call the class SuperString. Well start with the class demo project. Create a new Standard EXE project. Pull down the Add-Ins menu and select Class Builder Utility (if this choice is not available on the Add-Ins menu, use the Add-Ins Manager to make it available). The Class Builder dialog box will be displayed, as shown in Figure 6.3. The left side of the Class Builder dialog box lists the current project, which is called Project1 at present, because it hasnt been assigned a name. If the project contained any class modules, they would be listed below the project name. The tabs on the right list the properties, methods, and events of the class you are working on. Take the following steps:
In the Class Builder window, you can use the Methods and Properties tabs to view the elements you just added. We dont need to do this now, however, so select Update Project from the File menu or press Ctrl+S to save the newly created class to disk and add it to the project. Finally, Select Exit from the File menu to close the Class Builder utility. When you return to Visual Basic, look at the Project Explorer window. Youll see that the new class has been added to the project in a section called Class Modules. Youll also see that the SuperString class module has code associated with it. This is the code that was generated by the Class Builder utility in response to the information we entered. Well take a look at this code in a moment, but first, save your project. I used the name ClassDemo for both the form and the project. Now lets look at the code that was generated by the Class Builder utility, shown in Listing 6.1. Well need to modify this code, but the Class Builder has saved us some of the grunt work. Youll have a better understanding of the details of how the code works after completing this chapter and the class project in Chapter 8. For now, the following explanation will be sufficient.
How do property procedures work? When code outside an object wants to set a property, it will reference that property on the left side of an assignment statement. This causes the corresponding Property Let procedure to be called, with the value on the right side of the assignment statement passed as the argument to the procedure. For example, suppose we had created an instance of the SuperString class called buf. We would then set its value as follows: buf.value = Visual Basic When this line of code is executed, the Property Let procedure for the value property is executed, with the value Visual Basic passed as the procedure argument. In this case, code in the Property Let procedure simply assigns the value passed to the procedure to the private internal variable mvarValue, which is used to store the property. The sequence of events is similar for a Property Get procedure. When code wants to retrieve the value of the object property, it will reference the property on the right side of the assignment statement. For example: Text1.Text = buf.value
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |