Difference between revisions of "Visual Basic Modules and Procedures"
Line 13: | Line 13: | ||
We mentioned previously that Visual Studio places the code to construct each form in separate module files. We now need to learn how to create a new module in a project to contain our own Visual Basic code. Begin by creating a new Windows Application project in Visual Studio called ''vbModules''. Once the new project has been opened and the first form is visible, select ''Add Module...'' from the ''Project'' menu. The ''Add Item'' window will appear with the ''Module'' item pre-selected: | We mentioned previously that Visual Studio places the code to construct each form in separate module files. We now need to learn how to create a new module in a project to contain our own Visual Basic code. Begin by creating a new Windows Application project in Visual Studio called ''vbModules''. Once the new project has been opened and the first form is visible, select ''Add Module...'' from the ''Project'' menu. The ''Add Item'' window will appear with the ''Module'' item pre-selected: | ||
− | [[Image: | + | [[Image:Exampl.jpg]] |
Name the new module ''Math.vb'' and click the ''Add'' button. The new module will be added to the project a new tab for accessing the module code appears in the design area: | Name the new module ''Math.vb'' and click the ''Add'' button. The new module will be added to the project a new tab for accessing the module code appears in the design area: | ||
[[Image:Exampl.jpg]] | [[Image:Exampl.jpg]] |
Revision as of 17:13, 30 July 2007
A key part of developing applications using Visual Basic is ensuring that the code is carefully structured. This involves segmenting the code into projects, modules and procedures so that it is easy to understand and maintain.
A complete Visual Basic application is typically contained in a single project. Within a project, code is placed in separate code files called modules, and within each module, the Visual Basic code is further separated into self contained and re-usable procedures.
The topic of projects was covered in Creating a New Visual Basic Project. In this chapter we will look in detail at Visual Basic modules and procedures.
Visual Basic Code Modules
Visual Basic application source code is structured into module files with .vb suffix. By default, Visual Studio creates a separate module file for each form in an application containing the code to construct the form. For example, the code to create a form called Form1 will be placed in a module file named Form1.Designer.vb. Similarly, any code that has been defined by the developer to handle events from comtrols in the form will be placed by Visual Studio into a module file called Form1.vb.
When writing additional Visual Basic for an application, the code should ideally be logically grouped to together with other source code in a module file. When we say logically grouped we mean that the code should be grouped with other code of a similar nature. For example, code to work with files might be placed in a module called FileIO.vb, while mathematical procedures might all reside in a file name Math.vb. The idea is to ensure Visual Basic code is placed in a file where it make sense for it to be located. This will differ between applications, but it is worth investing the time to ensure code is well structured as doing so makes subsequent maintenance of the code by you or other developers much easier.
We mentioned previously that Visual Studio places the code to construct each form in separate module files. We now need to learn how to create a new module in a project to contain our own Visual Basic code. Begin by creating a new Windows Application project in Visual Studio called vbModules. Once the new project has been opened and the first form is visible, select Add Module... from the Project menu. The Add Item window will appear with the Module item pre-selected:
Name the new module Math.vb and click the Add button. The new module will be added to the project a new tab for accessing the module code appears in the design area: