In Visual Basic 6.0 Technology and Application (I) (hereinafter referred to as the above), we have discussed the theory of classes, the creation of classes, and the programming practices of class methods. In fact, the main reason why a class can be widely used in software engineering is that it can easily encapsulate many attributes required by programming, which not only allows programmers to overcome the control (OCX) to a certain extent) and link library (DLL) design and debugging complexity, and can improve the simplicity and efficiency of the program code-this article will discuss the complete class programming, including methods, attributes and basic events.
(1)Features and definitions of class attributes;
Similar to standard controls, class attributes allow users to assign values within a specified data range. These values are shared by various code parts in the class. The acquisition and transmission of attributes must be programmed through the property let and property get statements. Of course, we must first define the corresponding variables at the global or module level in the class.
(2)Event attributes and basic definitions;
Similar to the form event, the class also has two basic events, class_initialize (triggered when the class is loaded) and class_terminate (triggered when the class is uninstalled). Both events are private. In fact, we can ignore these two events-as long as you remember to improve the methods and attributes of the class.
Class can also define its own events. It is similar to the method programming format, except that the withevents keyword is required for parameter declaration, and the event cannot have any named or optional parameters, it also has no return value.
In fact, well-structured methods and attributes can completely replace complex class events.
(3)Programming examples of class methods, events, and Properties;
The purpose of this program is to control all the text box content in the form in upper case, lower case and reverse order conversion through the class.
To facilitate code writing and calling, I reference the enumeration Programming Method in the class.
The following code is used in the class1 class:
Option explicit
Private withevents mytxt as textbox
'Parameter interface of the Method
Public Enum Style
Lcaseit 'lower-case attributes
Lbigit 'uppercase attributes
Nlogoit' reverse sorting attribute
End Enum
'Custom enumeration' is used to automatically assign values to attributes.
Private mvarbiaozhi as style
'Implement the connection of enumerated Constants
Public Function done () as string'
The 'done method is used
'Form text box to convert the corresponding characters
'And returns the converted string
If mvarbiaozhi = nlogoit then
Done = strreverse (mytxt)
'Reverse sorting
Elseif mvarbiaozhi = lcaseit then
Done = lcase (mytxt)
'Force lowercase Conversions
Else
Done = ucase (mytxt)
'Forced capital transfer
End if
End Function
'Done method ended
Public property let biaozhi (byval vdata as style)
'Obtain the attribute value.
Mvarbiaozhi = vdata
End Property
Public property get biaozhi () as style
'Pass the property value to the class.
Set biaozhi = mvarbiaozhi
End Property
Public sub attach (ittext as textbox)
'Connection Class Method
Set mytxt = ittext
End sub
Private sub class_initialize ()
'The event is activated when the class times are loaded.
Msgbox "Hello! This program shows you how to program methods, attributes, and events of classes! "
End sub
Private sub class_terminate ()
'The event is activated when the class is detached.
Msgbox "Hello! Remember to enter the Undo object code in class_terminate! "
End sub
'Class code all ends
(4)Reference programming of form code;
In the form form1, add the text control text1, the drop-down list control combo1, and the command button command1 (Caption = "Start conversion") to adjust the three controls to the appropriate position.
Dim MYT as new class1
'Class reference
Private sub form_load ()
Combo1.Clear
Combo1.AddItem "string uppercase conversion"
Combo1.AddItem "string lowercase conversion"
Combo1.AddItem "string reverse sorting"
Combo1.ListIndex = 0
'Add attribute options to the list box
End Sub
Private Sub commandementclick ()
'Activate the class when the command button is pressed.
MyT. Attach Text1
'Method parameter join
Select Case Combo1.ListIndex
Case 0
MyT. Biaozhi = Lbigit
Case 1
MyT. Biaozhi = Lcaseit
Case 2
MyT. Biaozhi = Nlogoit
End Select
'Assign values to the Biaozhi attribute of the Class Based on the selection of the list box.
'Note: In the programming environment, the preceding attribute values are automatically added.
Text1.Text = myT. dONE
'Return the string after sorting.
End Sub
Private Sub Form_Unload (Cancel As Integer)
Set myT = Nothing
End
'Good programming habits
End Sub
Our code looks so concise that it is like using a control. It can be called as you like, and the vb Auto-Prompt function is convenient.
(5)Summary of the class Programming Technology;
Strictly speaking, classes are a very useful technology in VB programming and also a difficult point in learning and mastering. Classes are widely and effectively used in large-scale software engineering. However, in small software development, in order to improve software efficiency and code clarity, we should avoid using many class modules, controls and connection libraries instead of standard modules.
The sample code in this article is relatively simple, but covers all aspects of the module programming technology. I hope beginners can learn from it and programmers can also discuss it together. We should believe that no matter how complicated a high-rise building is made up of ordinary square bricks. Similarly, no matter What complicated software engineering is made up of basic program statements, the difference between programmers, programmers, and analysts is that different programs are built using the same program statement.