In the Technology and Application of Visual Basic 6.0 Classes (Part 1) (hereinafter referred to as the above), we discussed the theory of classes, the creation of classes and the programming practice of class methods. In fact, the reason why classes can be widely used in software engineering The most important thing about the application is that it can be very convenient to close Installing many attributes required for programming not only allows programmers to overcome the complexity in control (ocx) and link library (dll) design and debugging to a certain extent, but also improves the simplicity and efficiency of program code - this article will discuss Complete class programming, including methods, properties, and basic events.
(1) Characteristics and definitions of class attributes ;
Similar to the properties of standard controls, class properties allow the user to assign values within a specified data range, and these values are shared by various parts of the code within the class. The acquisition and transfer of attributes need to be programmed through PRpertyLet and PropertyGet statements. Of course, we first need to define the corresponding variables at the global or module level in the class.
(2) Attributes and basic definitions of events ;
Similar to the events of the form, classes also have two basic events, Class_Initialize (triggered when the class is loaded) and Class_Terminate (triggered when the class is unloaded), both of which are private. In fact, we can completely ignore these two events - as long as you remember to complete the methods and properties of the class.
Classes can also define their own events, which are similar to the programming format of methods, except that the WithEvents keyword is required for parameter declaration, and the event cannot have any named parameters or optional parameters, and it has no return value.
In fact, well-structured methods and properties can completely replace the events of complex-structured classes.
(3) Programming examples of class methods, events and properties ;
The purpose of this program is to control all uppercase, lowercase and reverse sorting of the contents of the text box in the form through classes.
In order to facilitate the writing and calling of code, I referenced the enumeration programming method in the class.
The following code is in class Class1:
OptionExplicit
PrivateWithEventsmyTXTAsTextBox
'Method parameter interface
PublicEnumsTYLE
Lcaseit' lowercase attribute
Lbigit' capitalization attribute
Nlogoit' reverse sort attribute
EndEnum
'Custom enumeration, used to implement automatic assignment of attributes
PrivatemvarBiaozhiAssTYLE
'Implement the connection of enumeration constants
PublicFunctiondONE()AsString'
'DONE method is used to set the
'Perform corresponding character conversion operations in the form text box
'And return the converted string
IfmvarBiaozhi=NlogoitThen
dONE=StrReverse(myTXT)
'Reverse sort
ElseIfmvarBiaozhi=LcaseitThen
dONE=LCase(myTXT)
'Force lowercase conversion
Else
dONE=UCase(myTXT)
'Force uppercase conversion
EndIf
EndFunction
'DONE method ends
PublicPropertyLetBiaozhi(ByValvDataAssTYLE)
'Get the assigned value of the attribute
mvarBiaozhi=vData
EndProperty
PublicPropertyGetBiaozhi()AssTYLE
'Transfer attribute values to the class
SetBiaozhi=mvarBiaozhi
EndProperty
PublicSubAttach(itTEXTAsTextBox)
'Method of connection class
SetmyTXT=itTEXT
EndSub
PrivateSubClass_Initialize()
'This event is activated when the class is loaded
MsgBox "Hello! This program shows you the techniques of programming using class methods, properties, and events!"
EndSub
PrivateSubClass_Terminate()
'This event is activated when the class is unloaded
MsgBox "Hello! Remember to fill in the object cancellation code in Class_Terminate!"
EndSub
'The code of the class is all over
(4) Reference programming of form code ;
Add text control TEXT1, drop-down list control COMBO1, and command button COMMAND1 (CAPTION="Start conversion") to form FORM1, and adjust the three controls to the appropriate positions.
DimmyTAsNewClass1
'Class reference
PrivateSubForm_Load()
Combo1.Clear
Combo1.AddItem"String capitalization conversion"
Combo1.AddItem "String lowercase conversion"
Combo1.AddItem "String reverse sort"
Combo1.ListIndex=0
'Add attribute options to the list box
EndSub
PrivateSubCommand1_Click()
'Activate the class when the command button is pressed
myT.AttachText1
'Method parameter connection
SelectCaseCombo1.ListIndex
Case0
myT.Biaozhi=Lbigit
Case1
myT.Biaozhi=Lcaseit
Case2
myT.Biaozhi=Nlogoit
EndSelect
'According to the selection in the list box, assign a value to the Biaozhi attribute of the class
'Note that in the programming environment, the above attribute values are automatically added
Text1.Text=myT.dONE
'Return the string after sorting
EndSub
PrivateSubForm_Unload(CancelAsInteger)
SetmyT=Nothing
End
'Good programming habits
EndSub
How about, our code looks so concise, it feels like using a control, which can not only be called at will, but also conveniently use the automatic prompt function of VB.
(5) Summary of class programming techniques ;
Strictly speaking, classes are a very useful technology in VB programming, and they are also difficult to learn and master. Classes are widely and effectively used in large-scale software projects. However, in small-scale software development, in order to improve software For efficiency and code clarity, you should avoid using more class modules, controls and connection libraries and replace them with standard modules.
The code example in this article is relatively simple, but it covers all aspects of module programming technology. I hope beginners can learn from it and programmers can discuss it together. We should believe that no matter how complex the high-rise buildings are, they are made of ordinary bricks. Similarly, no matter how complex the software projects are, they are composed of basic program statements. Programming enthusiasts and programmers The only difference from the analyst is that the programs constructed with the same program statements are different. ->