![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
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!
When this line of code is executed, the corresponding Property Get procedure is executed. Property Get procedures are actually functions, returning a value. In this case, code in the procedure simply returns the value stored in the internal private variable where the value property is stored. What is the point of using Property Get and Property Let procedures in a class? Why not simply declare one public variable for each property, so code outside the class could access that variable directly? The reason for doing things in what may seem to be an unnecessarily complicated manner is a principal called encapsulation. This principal states that the internal data and workings of a class should be as isolated as possible from the outside world. By requiring that access to a classs properties be done via property procedures, Visual Basic gives the programmer the opportunity to provide whatever degree of encapsulation is required. Well see this principal in action as we continue to work on our SuperString class. Listing 6.1 Code created by the Class Builder utility for the SuperString class. Option Explicit local variable(s) to hold property value(s) Private mvarValue As String local copy Private mvarLength As Long local copy Public Sub Insert(Txt As String, Position As Long) End Sub Public Property Let Length(ByVal vData As Long) used when assigning a value to the property, on the left side of an assignment. Syntax: X.Length = 5 mvarLength = vData End Property Public Property Get Length() As Long used when retrieving value of a property, on the right side of an assignment. Syntax: Debug.Print X.Length Length = mvarLength End Property Public Property Let Value(ByVal vData As String) used when assigning a value to the property, on the left side of an assignment. Syntax: X.Value = 5 mvarValue = vData End Property Public Property Get Value() As String used when retrieving value of a property, on the right side of an assignment. Syntax: Debug.Print X.Value Value = mvarValue End Property What modifications will we need to make to this code? Consider these three points:
Okay, lets get to work, dealing with the property procedures first. Select the SuperString class in the Project Explorer window and display its code for editing. Then, follow these steps:
Step 1, as Ive already mentioned, serves the purpose of making the Length property read-only. Steps 2 and 3 add code that calculates the length of the string stored in the Value property and stores the result in the Length property. Why do we need to perform this calculation in two different places? The reason for putting this line of code in the Property Get Length procedure is obviousto ensure that the Length property contains an accurate value when it is retrieved by the user. But why do we also need this code in the Property Let Value procedure? By doing so, the Length property will be updated each time the user changes the Value property, and therefore, will contain an accurate value even if the user has not specifically retrieved the Length property. The need for doing this will be obvious when we discuss the code for the Insert method. Now we need to turn our attention to the Insert method. The task of this method is to insert a new string at a specified position within the string currently stored. For example, if the SuperString object currently has Visual Basic stored in it, then calling Insert with the arguments XXX and 3 will result in the object having ViXXXsual Basic stored in it. Lets be specific about what the code in the method needs to do, paying particular attention to special cases:
The code for the Insert method is shown in Listing 6.2. You should be able to figure out how it works from the comments in the listing and from what you have learned already about Basic code. I will point out one thing: The code in this procedure needs to know the length of the original string stored in mvarValue, and it gets this information from the objects Length propertyin other words, the value stored in mvarLength. This is why we updated this value not only in the Property Get Length procedure, but also in the Property Let Value procedure. Otherwise, the Length property could be inaccurate. Listing 6.2 Code for the Insert method. Public Sub Insert(txt As String, position As Long) Inserts txt at position in mvarValue. Dim s1 As String, s2 As String If the new string is blank, exit. If txt = Then Exit Sub If the existing string is blank, replace it. If mvarValue = Then mvarValue = txt Exit Sub End If If Position is <= 1, tack new string on the beginning. If position <= 1 Then mvarValue = txt & mvarValue Exit Sub End If If Position equal or greater than the length of the existing string, tack the new string on at the end. If position >= mvarLength Then mvarValue = mvarValue & txt Exit Sub End If Otherwise stick it in the middle at the specified position. First get the left part of the old string up to but not including the character at position. s1 = Left(mvarValue, position - 1) Now get the right part of the old string from position to the end. s2 = Right(mvarValue, mvarLength - position + 1) Now put the pieces back together. mvarValue = s1 & txt & s2 End Sub We are almost done creating our SuperString class, but theres one more thing to doadd initialization code. This code is placed in the classs Initialize event procedure. This procedure is called when an instance of the object is first created, and you place code here to set the initial values of properties and perform other needed tasks. For the SuperString class, we will initialize the Value property to an empty string and the Length property to 0. Strictly speaking, these initialization steps are not needed, because Visual Basic automatically initializes string variables to an empty string and numeric variables to 0 when they are declared. Paying attention to class initialization steps is good programming practice, however, and you might as well start developing good habits early.
|
![]() |
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. |