![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
To add this code, display the SuperString class code in the Code Editing window. Then, at the top of the Code Editing window, select Class from the Object list and Initialize from the Procedure list. Enter the code shown in Listing 6.3. Listing 6.3 The Class_Initialize event procedure for the SuperString class. Private Sub Class_Initialize() Initialize the properties. mvarValue = mvarLength = 0 End Sub Using The SuperString ClassA class by itself is not terribly useful. You need to create instances of the classobjectsto do anything. Lets take a look at how this is done; then well create a simple demonstration program to show off the SuperString class. Creating Objects To create an instance of an object, you use the Dim statement. The general syntax is as follows: Dim varname As [New] classname Here, varname is the name you will use to refer to the object; it follows regular Visual Basic naming rules. Classname is the name of the class you want to instantiate. Ill explain the optional New keyword in a moment. Heres an example of using Dim to create an instance of a class: Dim SS1 As SuperString To be precise, this statement does not actually create an instance of the SuperString class, but only creates a variable that can refer to an instance. To create the object, you must execute the following: Set SS1 = New SuperString If, however, you use the optional New keyword in the Dim statement, then using the Set statement is unnecessary. In other words, the following code Dim SS1 As New SuperString has the same effect as these two statements: Dim SS1 As SuperString Set SS1 = New SuperString Why, then, would we ever need the Set statement? There are two cases. One is when, instead of declaring the object variable as a specific type, called early binding, you use the Object keyword and late binding. Late binding involves declaring the object variable as the generic Object type rather than as a specific class type, then referencing a specific class with the Set keyword: Dim MyObject as Object Set MyObject = New SuperString The second case is when you want to change the object reference of a variable. Suppose you had executed the following code: Dim SS1 As New SuperString Dim SS2 As SuperString Set SS2 = SS1 You would then have two variables, SS1 and SS2, that refer to the same object. This technique can be useful for some programming tasks. Late binding slows things down somewhat, but provides the added flexibility of being able to use the same object variable for different types of objects. For example: Dim MyObject as Object Set MyObject = New SuperString Do stuff here. Set MyObject = New SomeOtherClass As you can see, working with object references is a lot like working with ordinary Visual Basic variables, except for the use of the Set and New keywords. The Demonstration Program Now that you have seen how to create objects, we can return to the demonstration program that will use the SuperString class we created earlier in the chapter. We will use the same project that the SuperString class was created init already has a blank form. Start by designing the form, adding five Label controls, five Text Box controls, and one control array of three Command buttons. The captions for the Label controls should be set as shown in Figure 6.8. Assign the following names to the Text Box controls: txtInsert txtLength txtOriginal txtResult txtPosition Set the Text property of all the Text Boxes to a blank string, and position each Text Box next to the corresponding Label control. Assign captions to the three Command buttons as follows: Index 0: Process Index 1: Quit Index 2: Clear The final form is shown in Figure 6.8. Now lets turn to the code. Place the following statement in the General Declarations section of the forms code: Dim SS1 As SuperString
Next, place this code in the Form_Load event procedure: Private Sub Form_Load() Create the SuperString object. Set SS1 = New SuperString End Sub Finally, here is the code that goes in the Click event procedure for the Command buttons: Private Sub Command1_Click(Index As Integer) Dim C As Control Select Case Index Case 0: Process SS1 = txtOriginal.Text SS1.Insert txtInsert.Text, txtPosition.Text txtResult.Text = SS1 txtLength.Text = SS1.Length Case 1: Quit End Case 2: Clear For Each C In Form1.Controls If TypeOf C Is TextBox Then C.Text = Next C End Select End Sub Thats all there is to the program. Go ahead and run it. Enter some text in the Original String and String to Insert boxes, and enter a position in the Position box. Then click on the Process button. Youll see the resulting string and its length displayed, courtesy of the capabilities that we built into the SuperString class. Click on Clear to blank all boxes, and, of course, click on Quit to terminate. Lets take a closer look at what the code does. When the user clicks on the Process button, heres what happens:
Using an object that you designed is just as easy as using the objects that are provided with Visual Basic. But what about the code that is executed when the Clear button is clicked? Here, I am using one of Visual Basics built-in collections, a topic that we will cover in detail in Chapter 7. Ill explain only briefly here: Visual Basic automatically maintains a list of all the controls on each form in a project. Using the For...Each statement, you can loop through all of the controls on the list; using the TypeOf keyword, you can determine if each control is a Text Box. If it is, set its Text property to a blank string. This is the easiest and fastest way to clear all of the Text Boxes on a form.
|
![]() |
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. |