Next, we add an attribute to allow users to obtain the value of the customerid field. The corresponding sample code is as follows:
Public property get customerid () as string Customerid = RS ("customerid ") End PropertyPublic property let customerid (newvalue as string) RS ("customerid") = newvalue End Property |
Obviously, the get operation of this attribute simply returns the value of the "customerid" field. Correspondingly, the let operation sets a new value for the "customerid" field.
In other words, the property has two parts: "getting" and "leting". In fact, there may be another "setting" operation. But for different occasions, we always need get and let for read and write operations.
It is important to note that some values should be checked during the above attribute process. For example, when calling the let attribute, the user may perform the following operations:
| Objectname. customerid = "halfi" |
After this let attribute operation, "customerid" is equal to the new string "halfi ". However, when viewing the content of the northwind database, we will find that the character length of the "customerid" field cannot exceed 5. If the user has the following operations:
| Objectname. customerid = "halfistore" |
A database operation error occurs. Although the error handle can be used to solve this problem, is it better to check the length of newvalue in the code? If the value exceeds five characters, you can crop the first five characters or ignore the new string to bring up an error message. But here, we adopt the latter measure.
Add the following code to our class:
Public property get customerid () as string Customerid = RS ("customerid ") End Property Public property let customerid (newvalue as string) 'If the length of newvalue is greater than five If Len (newvalue)> 5 then '... Then raise an error to the program 'Using this class Err. Raise vbobjecterror + 1, "customerid", _ "Customer ID can only be up to five" & _ "characters long! " Else '... Otherwise, change the field value RS ("customerid") = newvalue End if End Property |
Well, before completing the following steps, we have spent a lot of time adding methods.
Add the following code to our class:
Public sub Update () Rs. Update End sub |
This update method simply calls the update method of the record set object to update the record.
Next, we will use a very small sample program to test this property and method. During the test, we will also use specific techniques to track the running of classes and programs.