How to Write High-Quality VB Code (2)

Source: Internet
Author: User

How to Write High-Quality VB Code (2)
12. Destroy objects
No matter what software you write, programmers need to consider releasing the memory space occupied by the software after the user decides to terminate the software operation. Unfortunately, many programmers do not seem very concerned about this. The correct method is to destroy the objects used in the program before exiting the program. For example:
Dim FSO as New FileSystemObject
Note: perform the operation
Note: destroy an object
Set FSO = Nothing
You can unmount the form:
Unload frmMain
Or
Set frmMain = Nothing
13. Variable Length and fixed length string
Technically speaking, compared with variable-length strings, fixed-length strings require less processing time and space. However, the disadvantage of fixed-length strings is that in many cases, you need to call the Trim function to remove the null characters at the end of the string, which reduces the code efficiency. So unless the length of the string does not change, the variable-length string is used.
14. Use class modules instead of ActiveX Controls
Unless ActiveX controls involve user interfaces, use lightweight objects such as classes whenever possible. There is a big difference in efficiency between the two.
15. Use internal objects
When ActiveX controls and DLL are involved, many programmers prefer to compile them and then add them to the project. I suggest you do not do this because connecting from VB to an external object requires a lot of CPU processing power. Every time you call a method or access a property, a large amount of system resources are wasted. If you have ActiveX controls or DLL source code, use them as private objects of the project.
16. Reduce the number of modules
Some people like to save common functions in the module, and I agree with this. But writing only 20 or 30 lines of code in a module is a bit ridiculous. If you do not need a module, try not to use it. This is because VB loads the module into the memory only when the function or variable in the module is called. When the VB Application exits, the modules will be detached from the memory. If there is only one module in the code, VB only loads the code once, improving the code efficiency. If there are multiple modules in the code, VB performs multiple loading operations, reducing the code efficiency.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.