Home
Site Structure

Programming Home
  Basic Home
    GUI & OS Home
    QB Knowledge Base
    Professor Answers
    Codename Surena
    QB Downloads

Write Us

Special Links
QB DirectoryNEW

Codename Surena
Forums
Sepent Basic GUI & OS Home
Register Log In
QB Knowledge Base > Technical Articles > Number 3

A Modular Programming Way in QB

In previous articles, we discussed the use of modules in structured programming, but we didn’t go into details of using them in QB programming. In this article, we will talk about using different kinds of modules to organize large projects properly.

Two Kinds of Modules
In QB, we have modules and include files. Based on their usage, we call the first group "Code Modules" and the other "Include Modules".

Include Modules
In QB traditions, these files usually have .BI extension. Include modules are mainly used for containing declarations of procedures and functions, variables, global variables (declared using COMMON statement), user-defined types and so forth. Include modules are quite useful for sharing declarations. Here’s a list of some of their most important uses:

  • When you have a code module containing several procedures, you should declare these procedures in any module that you want to use them in. You can put all the declare statements in an include module –that you may want to give it the same name as the code module with .BI extension. Then in each module you want to use the procedures you can put a $INCLUDE meta-command at the top of that module.
  • Another usage of include modules is sharing declarations of user-defined types. This means that you don’t need to declare the user-defined types in any module that uses them. Just put the declarations in an include module and include it in your code modules.
  • When you want to have a global variable accessible in all the modules, you need to declare the variables in all modules using a COMMON statement. Here again you can put the COMMON statements in an include module and include it in all the other modules.

There is one important point about include modules; Do not put executive commands in an include module. Doing so may cause errors if you put some non-executive commands (such as DECLARE, COMMON, etc.) after include statement.

Code Modules
Code modules are the means of splitting code discussed in the article "Coding Techniques". The main ideas of using these modules can be found in that article. These files -that in QB traditions have .BAS extension- form the main body of your project.

How to do it all?
We talked a lot about modules. But how can these techniques be used in QB? Here I’ll show you the way QB manages multiple modules. (Remember that early versions of QB do not support modules).

Each QB project contains a main module which has the starting point of the program. This startup code is the module-level code of the main module. Other modules should not have module-level code (In fact, they can; But the code will never be executed). Procedures in other modules are called from the main module (or from a procedure the main module calls).

To create a multi-module project from within QB environment, open your module (or create it), then from the File menu choose "Load File" or "Create File", enter (or choose) the module you want to add to the project, and select OK. The module will be added to the project. You can switch between modules by choosing Subs from View menu or by pressing F2. When you add a module to the project, QB creates (if necessary) a project file with the same name as the main module and .MAK extension, and adds the name of the modules to it. Then each time you open the main module, QB finds and opens the other modules (The project file should be in the same folder as the main module).

Conclusion
It is a great method to organize your project in modules. If you have not tried this method so far, try it in your next project and see the results.

By: Homayoon.P.A.
December 5, 2002