For programmers and programming enthusiasts, the technology in VB is a difficult point in learning. During the development of large software, the module (Moudle), Active OCX, Link Link (Active DLL) and class ( Class Moudle) constitutes a systematic, efficient software engineering, and the class's technology is the basis of control and link library technology, so the theoretical and programming methods of mastery are very meaningful.
(One)
Basic definitions and application overview of classes;
Class is a high-level code module that includes method, attribute, and data, which is within the scope of the module, is an Active OCX without a graphical interface. Programmers can use it as using controls, but can't see it. It is worth noting that the class cannot be inherited.
Class enables us to efficiently complete complex operations for one or a few specific objects, the action of the object is the method of the class, the object's properties are the properties of the class. Relatively speaking, if the programmed object is a group of things, then we use the standard module to be very suitable, in the following two cases, you should use the class for code processing:
(1) Create a large number of objects similar to;
(2) Improve the encapsulation of the code.
The creation of the class is very simple. When you write, you can add a blank class when you select the Add Category item in the Project menu.
Class files are typically saved as extensions in .cls.
(two)
The implementation of the method of class;
The method of class is similar to the interface function of the dynamic link library, which can accept the specified type parameters of other form code and pass to the class. Generally, the method of the class is to specify whether there is a return value. It is usually a public process in the class. Please see the following code example, which rejects a password box to refuse non-alphabetical inputs:
(1) Class CLS code;
Option Explicit 'variable check
Private Withevents Mytxt As TextBox
'This class accepts and controls a Text password box
DIM Isnum as boolean
'Class module variable
Public Sub Attach (Ittext As Textbox)
'Accept external variables into mytxt
Set mytxt = ittext
End Sub
Private sub mytxt_keyup (Keycode As Integer, Shift As Integer), SHIFT AS INTEGER
ISNUM = (Keycode> = 65) and (keycode <= 90)
'Test the keyboard input of the password box is English letters
IF isnum = false kil
BEEP
mytxt.text = ""
'If the input is not an English letter, the bell and clear the password box content
Msgbox "illegal character input!"
END IF
Debug.print mytxt.text
'Debug output password box content
End Sub
'Class code ends
(2)
Class reference;
The class that has been completed can be referenced by two formats. The first way: private (public or DIM) mycls (specified class name) AS New CLS (written completed class name); second way more for the program Writing a program more than "old": First, the module-level declaration in the form code - DIM MyCls AS CLS, and then performs specific definitions during the specific code - SET mycls = new CLS. In terms of the efficiency and code of the code, there may be differences, but there is no special feeling in the author's programming practice, but I use the first way because it is more convenient to write. In addition, when the code is ended, the resource occupied by using set mycls = nothing is a very good program habit. In the form form1 (the form has a password box control Text1, passworldchar = "*") Add the following code:
Option expedition
Private mycls as new CLS
'Reference CLS
Private sub flow_load ()
Mycls.attach Text1
'Starting class
End Sub
'Remember to release resources at the end of the code
Private Sub Form_Unload (Cancel AS Integer)
Set mycls = Nothing
End
End Sub
This document shows the code writing process and call mode of the class method (although it is very similar to the event of the class), its effect is that if the non-letter is entered in the password box, the system is ringing, and deletes the password box. The original data - protects the password to a certain extent.
The method of the class can not require any parameters, which is similar to a PUBLIC function or process, which is also the most widely used in the class. In the next article I will discuss, how to use the class's properties, events, and methods for comprehensive programming. (Endlessly)