Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!:Component Madness
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg) {
var end = document.cookie.indexOf (";", j);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(j, end));
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
var m1='';
var gifstr=GetCookie("UsrType");
if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; }
document.write(m1+m2+m3);
Keyword
Title
Author
ISBN
Publisher
Imprint
Brief
Full
Advanced Search
Search Tips
Please Select
-----------
Components
Content Mgt
Certification
Databases
Enterprise Mgt
Fun/Games
Groupware
Hardware
IBM Redbooks
Intranet Dev
Middleware
Multimedia
Networks
OS
Prod Apps
Programming
Security
UI
Web Services
Webmaster
Y2K
-----------
New Titles
-----------
Free Archive
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!
(Publisher: The Coriolis Group)
Author(s): Peter G. Aitken
ISBN: 1576102815
Publication Date: 08/01/98
function isIE4()
{
return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') );
}
function bookMarkit()
{
var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0";
parent.location.href=url;
//var win = window.open(url,"myitk");
//if(!isIE4())
// win.focus();
}
Search this book:
Previous Table of Contents Next
Designing The Class To 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:
1. Click on the Add New Class button on the Class Builder toolbar, or select New from the File menu, then select Class. The Class Module Builder dialog box will be displayed, as shown in Figure 6.4. Type SuperString in the Name box, then click on OK. The new class will be listed under the project name on the left side of the Class Builder utility.
Figure 6.3 The Class Builder utility.
Figure 6.4 Specifying the name of the new class.
2. Click on the Add New Property button or select File|New|Property. The Property Builder dialog box will be displayed, as shown in Figure 6.5. 3. Enter Value in the Name box and select String in the Data Type box. Click on the Default Property box to place a checkmark there, and leave the Public Property option set. Click on OK when finished. 4. Repeat Steps 2 and 3 to add another property with the name Length and the data type Long. For this property, do not check the Default Property option.
Figure 6.5 The Property Builder dialog box.
5. Click on the Add New Method button or select File|New|Method. The Method Builder dialog box will be displayed, as shown in Figure 6.6. 6. In the Name box, enter Insert. 7. Click on the button with the + on it, to the right of the Arguments box. The Add Argument dialog box will be displayed, as shown in Figure 6.7. 8. Enter Txt in the Name box and select String as the data type; then click on OK.
Figure 6.6 The Method Builder dialog box.
Figure 6.7 The Add Argument dialog box.
9. Repeat Steps 7 and 8 to add another argument with the name Position and the data type Long. 10. Back in the Method Builder dialog box, click on OK to close the dialog box and return to the Class Builder utility.
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.
The variables mvarValue and mvarLength are used to store the values of the classs two properties. Note that these are declared as Private, so that they are accessible only by code within the class module itself. A procedure named Insert takes one type String argument and one type Long argument, as we specified when using the Class Builder utility. Insert is empty at present, and we will have to write the code to provide its functionality. This procedure represents a method of the SuperString class. Note that it has been declared Public, so it can be called from outside the class. Two procedures named Value are specified as Property proceduresone is a Get and one is a Let procedure. A Property Let procedure is used by code outside the class to set the value of a property, and a Property Get procedure is used by code outside the class to retrieve the value of a property. The name of the proceduresin this case, Valuespecifies the name of the property. Property Get and Property Let procedures provide access to the Length property.
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: