Adding attributes and methods to the VB com basic lecture

Source: Internet
Author: User
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 Property

Public 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.