You can, however, create project files from within a special application. which .. is usually called a code generator. A code generator is an application that creates the code of an application based on user-supplied data. The various Wizards, for instance, are code generators; they generate code for the programmer. This code
is usually the skeleton of an application that you must modify according to your requirements.
The Project File
If you open the Calcs.vbp project file with a text editor, you’ll find the following references in it:
It contains a list of the Forms that make up the project and the Start-up Form (which is the Form displayed when the application starts). The project is stored in the Cales folder, and the Forms MathCalc and LoanCalc are stored in the Math and Loan folder, reSpectively. The references to these files from within the Cales project file are relative. Thus, if you move the entire parent folder with its subfolder to a pew location on your disk, the project file will still be able to locate the project’s components.
The Form File
The Form files (FRM) are also text files and contain the descriptions of the controls on the Form and the corresponding code. Here is the listing of the Calc.frm file:
(No, I didn’t create this listing with Visual Basic 5. The beta version I used to prepare this book inserts the line VERSION 5.0 at the beginning of the listing. The release version may use a different version number. It probably won’t change because the project structure has not changed from version 5 to version 6.)
The first definition is that of the Form, which begins with the line:
Begin VB.Form Calculators
VB:Form is a Form object, and Calculators is its name. Following this line is a list of Form properties. The properties whose names begin with “Client” determine the position and size-of the Form on the Desktop. The ScaleWidth and ScaleHeight properties determine the coordinate system of the Form. The positions of the controls on the Form are expressed in these units.
After the-properties of the Form and before the End keyword that closes the definition of the Form (the one that begins as Begin VB.Form),are the definitions of the controls. The following line:
Begin VB.CommandButton bttnExit
marks the beginning of the first Command button control on the Form, which is the Exit button. This definition ends with the End keyword. In between these two keywords are all the properties of the Exit Command button. Notice that the Font . property has a number of members and that they are all enclosed in a pair of BeginProperty/EndProperty keyw:ords.
The lines that correspond to the properties of the controls and the actual statements in the subroutines are omitted to make the structure of the file easier to see. Ellipses denote the places where properties and code lines would otherwise appear.
As you can see, writing an application that automatically generates Visual Basic code is straightforward. You have to start with a similar project, see what information Visual Basic stores in the project and Form files, and use these files as guides.
Moving and Copying Projects
Sooner or later you’ll have to move or copy a project to another folder. If you choose File >- Save Project As and save the project with the same (or a different) name in another folder, only the VBP file is stored in the new folder. The project’s components remain in their original folders. This may not be what you expected, but that’s how Visual Basic works. A project’s components need not reside in the same folder, so Visual Basic doesn’t copy all the files along with the project file.
You shouldn’t maintain multiple copies of the same file either. Suppose you create a custom Form for specifying colors. After this Form is tested and working, . you can use it run within multiple projects. This Form shouldn’t be replicated in .each project’s folder. If you decide to add a feature to it later (and believe me, you will), you’ll have to update multiple files. If you save this Form in a special folder though, you can add it to any number of projects. If you update the Form in a single folder, all the projects that use it will see the new Form.
This feature is why Visual Basic doesn’t enforce the one-folder-per-project rule. Create a folder for each project, and store all the files that are unique to the project in it, but add existing components to a new project from their original folders .
To save a project in a different folder, you must first copy all the files of the project to a new folder and, only then, save the project file in the same folder, If you first save the project to a new’ folder or under a different file name, your project won’t see the new files. Instead, it will refer to the original files.
As mentioned, the VBP file contains references to the project’s components. Of course, if you change-the name or path of even a single component . in your .project, Visual Basic will prompt you to save the project file before you close it. An awareness of this detail can save you a good deal of frustration.
You can also move projects to.a different folder from within the Windows Explorer. Visual Basic uses relative path names in the VBP file, so if you move all the files to a new folder, the relative references are valid. However, you shouldn’t count on this. It’s possible that the VBP file will end up seeing files other than those you think it does, or it might not find the referenced files at all. It’s best to use File menu commands when moving projects around. Select each component of the project in the Project Explorer window and save it under a different file name with the File >- Save As command. After you have saved all the components to the new folder, save the project in the same folder with the File >- Save Project As command.
So far, you have been executing applications within Visual Basic’s environment. However, you can’t expect the users of your application to have Visual Basic installed on their systems. If you develop an interesting application, you won’t feel like giving away the code of the application (the source code, as it’s called). Applications are distributed as executable files, along with their support files. The users of the application can’t see your source code, and.your application can’t be modified or made to look like someone else’s application (that doesn’t mean it can’t be copied, of course).
Applications designed for the Windows environment can’t fit in a single file. It just wouldn’t make sense. Along with the executable files, your application requires a number of so-called support files. If you’re using any custom controls, the files in which they reside (they have the extension OCX) must be distributed with the application.
In general, Windows applications require a large number of support files, and these files may already exist on many of the machines on which y.our application will be installed. That’s why it doesn’t make sense to distribute huge files. Each user should install the main application and the support files that aren’t already installed on their computer.
Creating an Executable File
Before preparing the setup application, you must create an executable file for your application. This me will be represented as an icon on your Desktop, and you can run the application without starting Visual Basic and loading the project. Simply double click the application’s icon on the Desktop (or a folder) to start the application.
To make an executable file for your project, follow these steps:
1. Choose File > Make project.exe (project is the name of the project).
2. Enter the name and the location of the file, and Visual Basic will create the executable file.
To set options for the executable files through the Project Properties dialog box, follow these steps:
1. Choose Project> Project Properties to open the Project Properties dialog box, shown.
2. Select the Compile tab.
Now you’re ready to specify options. Visual Basic can produce two types of executable files:
• Native code.
Compile to P-Code
When you select this option, Visual Basic compiles a project using p-code,’which is pseudo-code that the CPU can’t execute directly. BASIC has always been an interpreted language. Programs written in an interpreted language aren’t translated into machine language before they are executed. Instead, each like of code is translated as needed and then executed.
Interpreted programs aren’t as fast as compiled programs (which are translated into Optimized machine language before execution). A p-code program is somewhere in between the two. It’s highly efficient code, but it can’t be executed as is. A translation step is required. P-code, however, is closer to machine language than it is to Visual Basic, and the process of translating p-code to executable code is ‘ efficient.
Compile to Native Code
When you select this option, Visual Basic compiles a project Using native code, which is the machine language that the CPU understands and executes. The generated executable is faster than the equivalent p-code executable by as much as 20 times. This is a benchmark (a best-case scenario), and you shouldn’t expect such dramatic improvements with your average applications. Use this option for applications that perform involved math operations.
When you compile to native code, you have the following options:
• Optimizer Fast Code Maximizes the speed of the executable file by instructing the compiler to favor speed over size. To optimize the code, the compiler can reduce many constructs to functionally similar sequences of machine code.
• Optimize for Small Code Minimizes the size of the executable file by instructing the compiler to favor size over speed
• No Optimization Compiles without optimizations
• Favor Pentium Pro” Optimizes code to favor the Pentium Pro processor. Use this option for programs meant only for the Pentium Pro. Code generated with this option runs on other Intel processors, but it doesn’t perform a~..well as if compiled with other options . Create Symbolic Debug Info Generates symbolic debug information in the executable. An executable file created using this option can be debugged with Visual.C++ or with debuggers that use the CodeView style of debug information. Setting this option generates a PDB file with the symbol information for your executable. This option is most likely to be used by VisualC++ programmers who also use Visual Basic.
Selecting Advanced Optimization Options
When you select.Advanced Optimizations, Visual Basic opens the Advanced Optimizations dialog-box, shown. You use these options to turn off certain checks that normally take place and ensure that your application works properly. To increase the speed of the executable file, you can turn off some or all of these checks by selecting the appropriate checkbox.
Assume No Aliasing
Select this checkbox to tell the compiler that you to program doesn’t use aliasing. Aliasing is a technique that lets your code refer to a variable (memory location} by more than one name. This technique is not used in this book, and you can ‘safely select this option ..
Remove Array Bounds Checks
By default, Visual Basic checks an array’s bounds every time your code accesses the array to determine if the index is within the range of the array. If the index is not within array bounds, a runtime error is . generated (which can be trapped from within the code). Select this option to turn off the array bounds checking and speed ,up applications that use arrays. However, the code that ensures that the array’s bounds aren’t exceeded may cost more in execution time. If an array bound is exceeded, the results will be unexpected.
Remove Integer Overflow Checks
By default, Visual Basic checks every calculation for integer-style data types-Byte, Integer, and Long-to ensure that the value is within the range of the data type. If the iagnitude of the value being put into the data type is incorrect, a runtime error l~ generated:Select this option to turn off error checking and speed up integer calculations. If data type capacities are overflowed, you’ll get incorrect results,
Remove Floating Point Error Checks
By default, Visual Basic checks every calculation of a floating-point data type-Single and Double-to be sure that the value is within range for that data type and that there are no divide-by-zero or invalid operations. If the magnitude of the value being put into the data type is incorrect, an error occurs. Select this option to turn off error checking and speed up floating-point calculations. If data type capacities are overflowed. ne:;error occurs, and you’ll get incorrect results.
Allow Unrounded Floating Point Operations
When this option is selected, the compiler uses ‘floating-point registers more efficientlY,avoids storing and loading large volumes of data to and from memory, and compares floating points more efficiently.
Remove Safe Pentlum™ FDIV Checks
Selecting this option removes the safety checking so that the code for floating-point division is faster, but may produce slightly incorrect result:s on Pentium processors with the FDIY bug .