590 The Structure of a BASIC Program Basic, which makes it possible to translate or port a Visual Basic program into REALbasic so you can create a Linux or Macintosh version of a Visual Basic program. Both Visual Basic and REALbasic are the most prominent BASIC dialects that offer structured programming and object-oriented programming. Two simpler BASIC dialects that omit object-oriented features are Liberty BASIC (www. libertybasic.com) for Windows and Chipmunk BASIC (www.nicholson. com/rhn/basic) for the Macintosh. You can also visit the Run BASIC site (www.runbasic.com) to practice writing BASIC programs through any browser. The Structure of a BASIC Program In the original BASIC language, a program consisted of one or more commands listed one after another, such as PRINT “This is a simple BASIC program” END For creating simple programs, BASIC lets you get started right away by focusing only on the commands you need. To create more complicated programs, you can create a single main program and one or more subprograms. The main program always runs first and determines when to run commands stored in different subprograms. In both Visual Basic (VB) and REALbasic (RB), there’s no main program. Instead, a typical VB/RB program consists of a single project file that lists the names of all files that make up that program. The three main types of files used in a project are ✦ Window files ✦ Module files ✦ Class files Using windows files A window file contains a single window and various controls that appear on that window, such as buttons, pull-down menus, or sliders. Besides containing the user interface, a window file also contains subprograms that tell each control, on that user interface, how to respond to an event. Such subprograms, or event subprograms, tell the computer how to respond when the user does something, such as click the mouse or move the mouse over a control. Unlike a traditional BASIC program where the main program
The Structure of a BASIC Program 591 determines when and how many times a subprogram runs, a VB/RB program consists of multiple subprograms that may run in the order determined solely by what the user does. Book VI Chapter 5 A Visual Basic or REALbasic program typically consists of at least one window file where you place controls, such as buttons, check boxes, or sliders, as shown in Figure 5-1. PC–Diet Plan Visual Basic and REALbasic Let’s plan a meal for today: Figure 5-1: Creating a Visual Basic or REALbasic program involves placing controls on a window. Red meat Fish Carrots Broccoli Salt OK Poultry Something made from tofu Asparagus Brussel sprouts Quit Stored inside each window file are event subprograms that tell the computer how to respond when the user clicks or manipulates that control. To create an event subprogram, double-click a control. This creates a blank event subprogram that you can fill with commands that tell the computer what to do. Using module files A module file is optional but is often used to store commonly used subprograms. By storing subprograms in separate module files, you can create a library of subprograms that you can copy and plug in to another Visual Basic/REALbasic project. Typically, a VB/RB project consists of zero or more module files. Using class files A class file contains BASIC code to define one or more classes, which is used in object-oriented programming to create objects. A typical Visual Basic/ REALbasic project consists of zero or more class files. Each time you create a new object, create a new class file. As a result, if you use object-oriented features, your project can consist of dozens of separate class files.