How to write high quality VB code (2) 12. The destruction of the target is written, and the programmer needs to consider the memory space occupied by the user decided to terminate the software after the software is released. But unfortunately, many programmers seem to be very concerned about this. The correct approach is to destroy the objects used in the program before exiting the program. For example: DIM FSO AS New FileSystemObject Note: Execution Operation Note: Destroy Object SET FSO = Nothing For the form, you can uninstall: unload frmmain or set frmmain = Nothing 13. Growth and fixed-length strings From technology, change Compared to long strings, the fixed length string requires less processing time and space. But the disadvantage of the fixed length string is that in many cases, you need to call the Trim function to remove the empty character of the string, which will reduce the code efficiency. Therefore, unless the length of the string does not change, otherwise it is still used. 14. Use class modules instead of ActiveX control unless ActiveX control involves the user interface, it is possible to use lightweight objects, such as classes. The efficiency between the two is very different. 15. When using internal objects involving using the ActiveX controls and DLL, many programmers like to compile them and join the project. I suggest you do not do this because it takes a lot of CPU processing capabilities from VB to an external object. Whenever you call methods or access properties, you will waste a lot of system resources. If you have an ActiveX control or DLL source code, use them as a private object of the project. 16. The number of reduction modules Some people like to save the universal function in the module and agree with this. But in a module, I only write a two-30 line code. If you are not a very needed module, try not to use it. The reason is because only the modules are loaded into memory only when the functions or variables in the module are called; these modules are uninstalled from memory when the VB application exits. If there is only one module in the code, VB will only load operation, so the efficiency of the code is improved; if there are multiple modules in the code, VB will perform multiple load operations, the efficiency of the code will decrease. 17. When using an object array When designing a user interface, for the same type of control, programmers should try to use an object array as much as possible. You can do an experiment: add 100 Picturebox on the window, each PictureBox has a different name, running the program. Then create a new project, and add 100 Picturebox on the window, but this time you use an object array, run the program, you can notice the difference in two programs loaded. 18. When you change the location of the object using the MOVE method, some programmers like to use Width, HEIGHT, TOP, and LEFT properties. For example: image1.width = 100Image1.Height = 100Image1.top = 0image1.Left = 0 This is actually very efficient, because the program modifies four properties, and each modification, the window will be regenerated. The correct approach is to use the Move method: image1.move 0,0,0,100,100 19. Reduce the image of the image will take up a lot of memory, and the image also requires a lot of CPU resources. In the software, if possible, consider replacing the picture with a background color - of course, this is just from the perspective of the technician.
20. Using the ActiveX DLL instead of the ActiveX control if the ActiveX object you designed does not involve the user interface, use the ActiveX DLL. Compiling Optimization I have seen many VB programmers never use compilation options, nor did they try to figure out the difference between the various options. Let us take a look at the specific meaning of each option. 1. P-code (pseudo code) and native code You can choose to compile the software as a P-code or this machine code. The default option is this unit code. What is P-code and this unit? P-code: When executed in VB, VB first is to compile the code as a P-code, and then explain the P-code to perform compilation. In the compilation environment, use this code to be faster than this code. After selecting the P-code, it is compiled when VB puts the pseudo code in an EXE file. This machine code: The native code is the option to be launched later in VB6. When compiled into an EXE file, the execution speed of the unit is faster than the P-code. After selecting the unit code, the VB is compiled using the machine instruction to generate an EXE file. When compiling using this unit code, I found that sometimes it will introduce some inexplicable errors. My code is fully implemented in the compilation environment, but the EXE file generated with this unit code option cannot be executed correctly. Usually, this happens when the uninstall window or pops up the print window. I solved this problem by joining the DOEVENT statement in the code. Of course, this situation has very little chance, maybe some VB programmers have never encountered, but it does exist.