For programmers and programming enthusiasts, the technology of classes in VB is a difficult point in learning. In the development process of large-scale software, modules (Moudle), controls (Activeocx), link libraries (Activedll) and classes (Classmoudle) It constitutes a systematic and efficient software engineering, and class technology is the basis of control and link library technology. Therefore, it is very meaningful to master the theory and programming methods of classes.
(1) Basic definition and application overview of classes ;
A class is a high-level code module that contains methods, properties, and data members. It is not only within the scope of the module, but also an Activeocx without a graphical interface. Programmers can use it like a control, but they cannot see it. It is worth noting that classes cannot be inherited.
Classes enable us to efficiently complete complex operations on one or several specific objects. The actions of the object are the methods of the class, and the attributes of the object are the attribute processes of the class. Relatively speaking, if the object of programming is a group of things, then it is very appropriate for us to use standard modules. In the following two cases, classes should be used for code processing:
(1) Create a large number of objects with similar properties;
(2) Improve the encapsulation of code.
Creating a class is very simple. When writing code, select the "Add Class Module" item in the "Project" menu to add a blank class.
Class files are generally saved with a .cls extension.
(2) Implementation of class methods ;
The method of a class is similar to the interface function of a dynamic link library. It can accept specified type parameters from other form codes and pass them to the class. Generally speaking, class methods can specify whether there is a return value. It is usually a public procedure in the class. Consider the following code example, which causes a password box to reject non-letter input:
(1) cls-like code;
OptionExplicit' variable check
PRivateWithEventsmytxtAsTextBox
'The methods in this class accept and control a text password box
DimisNUMAsBoolean
'Module-level variables of the class
PublicSubAttach(itTEXTAsTextBox)
'Accept external variables into mytxt
Setmytxt=itTEXT
EndSub
PrivateSubmytxt_KeyUp(KeyCodeAsInteger,ShiftAsInteger)
isNUM=(KeyCode>=65)And(KeyCode<=90)
'Test whether the keyboard input in the password box is English letters
IfisNUM=FalseThen
Beep
mytxt.Text=""
'If the input is not an English letter, the bell will ring and the contents of the password box will be cleared.
MsgBox "Illegal character input!"
EndIf
Debug.Printmytxt.Text
'Debug output password box content
EndSub
'End of class code
(2) Class reference ;
The classes that have been written can be referenced in two formats. The first way: Private (public or dim) myCLS (specified class name) AsNewcls (the written class name); the second way is more suitable for programming style. Older programmers: first make a module-level declaration - DimmyCLSAscls in the form code, and then make a specific definition - Setmycls=Newcls in the specific code process. There may be differences in the efficiency and simplicity of the code between these two methods, but in the author's programming practice, there is no special feeling. However, I prefer the first method because it is more convenient to write. In addition, at the end of the code, it is a very good programming habit to use SetmyCLS=Nothing to cancel the resource occupation of the class.
In the form form1 (the form has a password box control text1, passworldchar="*") add the following code:
OptionExplicit
PrivatemyCLSAsNewcls
'Quote cls
PrivateSubForm_Load()
myCLS.AttachText1
'Startup class
EndSub
'Remember to release resources at the end of the code
PrivateSubForm_Unload(CancelAsInteger)
SetmyCLS=Nothing
End
EndSub
The code in this article shows the code writing process and calling method of the class method (although it is very similar to the event of the class). Its effect is that if a non-letter is entered in the password box, the system will ring and the password box will be deleted. of the original data - protecting the password to a certain extent.
A class method does not require any parameters, which is similar to a public function or procedure. It is also the most widely used in classes. In the next article I will discuss how to use the properties, events, and methods of classes for comprehensive programming. (To be continued)
->