![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
When you select the Native Code option, you have several more options available. The ones you need to know about are listed here:
If you are unsure of which compilation options to choose, I suggest that you select the Compile to Native Code and the Optimize for Fast Code options. Variable ScopeBefore ending this chapter, we need to explore the concept of variable scope. The scope of a variable refers to the parts of the program where it is visible. When a variable is not visible, or is out of scope, it may as well not exist. True, it still exists, but somewhere out of your reach. You wont be able to access it again until it comes back in scope. The concept of scope applies to procedures also. What do I mean by parts of a program? In a Visual Basic module, some code is contained in procedures (both general and event procedures) and is called procedure level code. Most of the code in the Calculator project is procedure level code. Other code (such as Calculators Const declarations) exists outside any procedure and is called module level code. Because some Visual Basic programs contain more than one module, identifying which one the module code is within is also important. The location of a variables declaration (its Dim statement) is one factor that determines the scope of a variable. If you declare a variable inside a procedure, its scope is limited to that one procedure. Module level code cannot access it, nor can code in other procedures. Such variables are called local variables, because their scope is local to the procedure where they are declared. In fact, different procedures can have local variables of the same name. They exist as totally independent variables because their scopes do not overlap. For example, we used a variable named Result in more than one procedure in the Calculator project. If you declare a variable in module level code, its scope is determined by the keyword used to declare it. If you use the Dim statement or use the Private keyword, the scope of the variable will be limited to that single module. Thus, the following two statements in module level code have the same effect: declaring a variable whose scope is limited to that one module: Dim X As Integer Private Y As Double If you use the Public keyword in a module level declaration, the variables scope will be the entire project (all modules). You can also use the Global keyword with the same effect: Public X As Integer Global Y As Double You cannot use the Public or Global keywords within a procedure. You may wonder why programmers dont simply make all their programs variables publicwhy worry about scope? Bad idea! Dont look at scope as a restriction, but as a tool that helps you create bug-free programs. The ability to hide variables in procedures from the rest of the program is a major component of the power of structured programming. With local variables, a procedure can be completely independent from the rest of the program. It receives information via its arguments, and returns information (if it is a function) via its return value. No other part of the program can screw up what goes on in the procedure. Likewise, what goes on in the procedure cannot screw up the rest of the program. The rule to follow is to use local variables as much as possible. Public variables should be reserved for situations where they are really needed. The Visual Basic Application WizardIf you have used Microsoft software products, you may have encountered wizards. A wizard is a tool that helps you perform some task. Typically, a wizard walks you through a series of steps, asking you questions and offering choices related to the task at hand. Then, it carries out the specified actions, performing a lot of work that you would otherwise have to perform yourself. One of Visual Basics neatest features is the Application Wizard. It takes you through the steps of creating a Visual Basic application; then, based on your responses, it creates the skeleton of the program. You must still write the code and design the forms that provide the applications unique functionality, but a lot of the busy worksuch as menus and toolbarswill be done for you (at least in their basic form). Using the Application Wizardor any wizardis a fairly simple matter. I will not attempt to explain all the choices that the wizard offers you, for two reasons. First, the wizard dialog boxes are usually quite clear, with explanatory notes for the various choices. Youll probably be able to figure out most, if not all, of the options on your own. Secondly, some of the project choices offered by the wizardsuch as menus and multiple versus single document interfaceshave not been covered yet. Therefore, I will explain the basics of using the Application Wizard without going into the details. As you read further in the book, you can experiment on your own to explore its full capabilities. To start the Application Wizard, display the New Project dialog box by selecting New Project from the File menu. Select the Visual Basic Application Wizard icon and click on OK. The wizard will display the first of its dialog boxes (an example is shown in Figure 5.11). While the details of each wizard dialog box differ, they all have the following buttons:
Once the wizard has completed its work, you will have the basis for your Visual Basic application with all of the selected elements in place. A wizard-created application is the same as one that you created yourselfjust a lot easier. One nice touch is that the wizard can insert to do comments in the code, reminding you of the code that needs to be added for various functions.
What Now?With the completion of this chapter, you have finished the first part of the book. Youve come a long way and learned a lot. The material we have covered so far has enabled you to do a great deal with Visual Basic, but we havent even touched on a lot of important stuff: menus, common dialogs, class modules, and much more. From here on, however, the approach will be much less general. Each chapter will delve into a specific area where you might want to apply Visual Basictext processing and multimedia are two examples. Well deal with the remaining Visual Basic tools as we go along.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |