Hour 1


During this hour you will learn

Introducing Visual Basic

Welcome to night school with Visual Basic! You've decided to go the extra mile and learn Visual Basic programming in your extra time. Visual Basic Night School encompasses a power-packed course, amplified by the Internet, so that you can learn to program with Visual Basic and make your time investment worthwhile.

Microsoft Visual Basic can be simple or complex, depending on your background. To truly master Visual Basic, you have to tackle both the easy and difficult parts. This hour's lesson gets you started by describing the background you need to understand where Visual Basic fits in with other programming products.

Topic 1: The Programming Process

Why bother with Visual Basic? After all, if you want your computer to do something, you only have to go to the store and buy a program.

Visual Basic enters the picture when you can't find a program that does exactly what you want done. For example, you might want a program that manages your company's accounting records exactly the way you've done business for years, yet all the accounting programs you find in the stores manage books slightly differently. You might want to play a certain kind of computer game that you've thought of but nobody else has come up with, so you're stuck without it unless you write the game yourself.

In this section, you'll learn why programming is important, the history of programming languages, and why Visual Basic offers benefits over other languages you might choose. This first section is the longest in this hour because, despite the short history of programming compared to other fields, programming has evolved through several distinct stages that are still important if you want to understand why programming tools work today the way they do.

FAST TRACK
If you already know what programming languages do, jump to "Topic 2: Visual Basic's Nature", to learn the specifics of Visual Basic.

Overview

Before diving into Visual Basic, you should take the time to learn a little about the history of computer development tools that came before Visual Basic. Visual Basic (sometimes called VB) is one of the most advanced programming systems on the market today-yet Visual Basic is one of the simplest ones to use.

This topic section won't look at the specifics of Visual Basic yet. At this point-especially if you've never written computer programs before-it would be difficult to see the overall picture if you began studying Visual Basic's details. Therefore, this topic section begins by exploring the need for development systems such as Visual Basic, and explains the historical events that led to Visual Basic's development.

Looking at Programs

You already know what a program is. When you purchased your computer, the seller probably loaded your computer with lots of introductory programs, such as card games, personal finance programs, and system-related programs.

When you want your computer to do something, you must supply a program for the computer. Before discussing the programming process in more detail, you need to understand what a program is from the computer programmer's perspective (as opposed to a computer user's perspective): A program is a set of detailed instructions that tells the computer what to do.

Some day, you might be able to talk to a computer, and the computer will do what you want. In the meantime, you can't even type commands into the computer unless those commands follow a rigid syntax (grammar and spelling), and the computer can then handle only single instructions as you type them (such as MS-DOS commands). A program lets you group such instructions together to do more than a single task.

Computers Are Dumb

Even the most powerful and expensive computers built today lack intelligence. They lack all intelligence. Your computer is no more capable of doing something on its own than your car is capable of driving to your office without you.

Computers are just boxes with wires that connect millions of switches to each other. Without a program, your computer wouldn't know how to do anything.

A program is really only temporary in most computers. The program loads into memory and teaches the computer how to do something, such as be an electronic worksheet. Then, when you turn off the computer, its memory is erased and the computer comes back on with a fresh (and dumb again), blank program space. Although you can set up your computer to start certain programs when you power on the computer, such action hides the fact that the computer begins each power-on with empty memory.

If you want the computer to perform a fairly complex task, such as balance your checkbook or play a game, you must enter lists of instructions. Those instructions make up a program, and the program is the instruction collection that does a job.


A program can be complex, and you have to get the program just right before Visual Basic runs the program. Expect mistakes! Visual Basic makes it easy to locate and correct mistakes. One of the most common new-programmer misconceptions is that a computer is broken when it's the programmer's program that has problems, and the programmer needs to fix the mistakes.

The History of Programming

Over the years, developers have been developing computer programming tools that help make programming easier. In the old days of the 1940s and '50s, programmers often entered cryptic instructions called machine language and assembler language that made the computer do work. Computers were difficult to program in those early days, even though the computer programs were much less complex than today's programs. The old programming methods were time-consuming and difficult to get correct, so lots of computer errors (called bugs) appeared.


Machine language is a language with only two commands: on and off. A machine-language program is nothing more than a collection of 1s and 0s representing the states of the millions of two-state (binary) switches inside the computer.
Don't worry too much about machine language, because if you're lucky, you'll never need to learn it! Today's tools-especially advanced development systems such as Visual Basic-make using machine language obsolete and a waste of time, except for the deep understanding of computers such language might produce.

Machine language and assembler language are called low-level languages. Developers needed easier ways to program computers than the low-level languages provided. The problem, however, is that computers understood only machine language. (Assembler language is just a superset of machine language that hides the fact that a programmer is using binary values to create a program. Therefore, assembler language is only a slight improvement over machine language.) How would you enjoy writing a program that calculates a square root using machine language's 1s and 0s? The program might start like this:

01000111

10100011

01110110

10010010

10011001







Such programs obviously lend themselves to trouble and are extremely difficult to understand. Even today, computers understand only machine language at their lowest levels. Nevertheless, computer developers have put buffer languages between you and the machine language, so that you can enter high-level instructions and let the computer convert those instructions to the needed machine language. Only after converting the language you use into machine language can the computer eventually follow your instructions and do the program's job. (This lesson's final topic, "Compiling and P-Code," explains the process of this conversion in more detail.)

Although computers can't understand spoken languages yet, we can program computers by using high-level programming languages. One such high-level language is Visual Basic, but Visual Basic is so much more advanced than the early high-level languages were. Perhaps you've heard of some of the following high-level programming languages:

Each of these languages is much easier to work with than machine and assembler language, because high-level languages use commands that you can understand, such as Print and Get. You must admit, typing Print is much simpler than entering a bunch of 1s and 0s.

All these languages (and additional less-common ones) worked well and were used as the major high-level languages used through the 1980s. The late 1980s, however, brought new computer challenges that these languages couldn't handle well. Windowed environments, such as Microsoft Windows, made writing programs in these older languages difficult. These programming languages worked well in the day's text-mode environment but not for the new Windows environments. You'll learn why windowed graphical environments present challenges as you progress through your Visual Basic night-school training.

As you might suspect, Microsoft based Visual Basic on the older BASIC programming language. The next section explains why Microsoft gambled on BASIC for Visual Basic's foundation.

BASIC: Visual Basic's Roots

Whereas BASIC is a language suited solely for text-mode environments and sorely suited for graphical environments, Visual Basic includes almost the full original BASIC language. Although Visual Basic goes far beyond BASIC and makes BASIC suitable for windowed environments, one of Visual Basic's greatest strengths is its BASIC foundation.

The name BASIC is an acronym for Beginner's All-purpose Symbolic Instruction Code. In the 1960s, some Dartmouth College professors designed BASIC for their students. Students balked at learning some of the more detailed programming languages of the day, such as FORTRAN and COBOL, because, understandably, those languages are often difficult for non-computer students. The Dartmouth professors wanted their non-computer students to be able to write quick-and-simple programs without worrying too much about the details required from the other languages of the day.

Throughout the years, companies added to the original BASIC language and made it more powerful while still keeping the ease of use. Businesses and technical programmers rarely adopted BASIC, however, because it was known as a beginner's language and too simplistic for real-world programming. Just when it looked as though BASIC might become obsolete, Microsoft made a fateful decision-rather than toss BASIC, Microsoft recharged the language with these goals:

The result was Visual Basic. Although Visual Basic is based on BASIC, a high-level computer programming language for beginners, you have several nights ahead of you before you'll grasp all the Visual Basic concepts and before you'll be very productive with Visual Basic. Nevertheless, unlike most other Windows programming languages, such as C and C++, Visual Basic retains enough of BASIC's ease of use to make Visual Basic one of the simplest tools to learn for the Windows programming environment.


Visual Basic isn't easy just because of the underlying BASIC-like language. Visual Basic is simple because so much of your program development will consist of arranging graphical icons and controls on-screen while using a Paint-like program editor. In older languages, you'd have to write detailed input commands; a Visual Basic programmer, on the other hand, simply drags and drops a control such as a text box, scroll bar, or command button to a Visual Basic screen, adjusts a few details about the control, and lets Visual Basic take care of all the tedium. Much of a Visual Basic program's development requires designing the user interface and choosing the right tools needed.

Microsoft Visual Basic is the official name of the programming language you'll learn here, but Visual Basic isn't just a language-it's a complete development environment that includes the language, editor, program test environment, graphical controls, and online help. Therefore, when you learn Visual Basic, you must learn more than a new set of commands, but you must also master the development environment. Fortunately, Microsoft makes mastering the Visual Basic environment easy!

Example

Back when computers had only a few bytes of memory, programming in machine language didn't actually pose the same problems that machine-language programming produces today. Perhaps the most difficult job needed for those early computers might be to average a list of numbers. Many early computers had a couple of hundred bytes of memory at most. To write a program in machine language, you wouldn't even sit in front of a keyboard; you would sit in front of a switch panel.

Early programmers used a switch panel to program computers.

As computer designers learned how to add more memory to the computers, machine language became too complex, so they added a keyboard and began using assembler language to help mask the 1s and 0s. Listing 1.1 shows a sample from an assembler language program. The codes, called mnemonics, represent a different pattern of 1s and 0s that make the computer do something. The mnemonics each stand for something. For example, MOV represents a combination of 1s and 0s that instruct the computer to move data from one memory location to another. MOV is easier to remember and less bug-prone than having to type the specific series of 1s and 0s that represent the data-movement command.

Listing 1.1 Sample Code from an Assembler Language Program

FDIVR   DWORD PTR [BX+DI+001E]

REPZ

STOSB

MOV     [D82E],AX

MOV     [D724],AX

XCHG    DI,AX

AND     [SI],BX

OR      [BX+0021],DX

ADD     [BP+SI],AH

ADD     [BX],CH

DAS

OR      AL,FD

ADD     DH,[BP+SI]

XOR     AL,00

JL      0141







Assembler language was a huge step forward and further promoted computer purchases. Businesses and government installations began buying computers and putting them to use. The widespread growth of computers required a better programming system than assembler language. Although assembler language was one step closer to spoken language than machine language, the one-to-one correspondence between assembler language and machine-language instructions made the low-level assembler language too bulky to use for the larger applications than institutions need.

Therefore, developers began writing high-level languages, such as FORTRAN, C, and BASIC, that weren't closely linked to the underlying machine language. Although the computer has to convert the high-level language to machine language before running the program, programmers don't have to do as much work to write and enter programs. Listing 1.2 shows a simple BASIC program. Although the language is still somewhat cryptic, the commands make much more sense than assembler language and especially more sense than machine language.

Listing 1.2 A More Easy-to-Understand BASIC Program

10 PRINT "This program will calculate the area of a circle."

20 PRINT

30 INPUT "What is the circle's radius"; R

40 P = 3.14159

50 A = P * R * R

60 PRINT "The circle's area is"; A

70 END








The 7-line BASIC program in Listing 1.2 might require 100 lines of assembler language or-worse-several thousand machine-language 1s and 0s to perform the same work! Obviously, high-level languages make a programmer's job easier.

Visual Basic takes the ease of BASIC a step further. Not only is Visual Basic a language that's greatly improved over BASIC (and most other modern and classic programming languages), Visual Basic includes the following components:

Without explaining any of the details (yet) of Visual Basic programming, Figure 1.2 gives you a hint of things to come. Figure 1.2 shows the Visual Basic development system during the creation of a program. Things got busy fast, didn't they? The BASIC language that was making things simpler now seems to become convoluted with Visual Basic!

Figure 1.2

The Visual Basic screen includes lots of things happening.

Visual Basic really didn't complicate programmer's jobs. Despite Figure 1.2's complex-looking screen, Visual Basic helps streamline Windows programming jobs. Windows brings many new requirements to a program that non-windowed environments don't require. With those requirements, however, comes a ton of benefits that the rest of this book will explore.

Next Step

To prepare you for "thinking in Visual Basic," consider the following differences between a text mode (non-Windows) and a Windows environment.

In a text-mode environment,

In a Windows environment,

A development system such as Visual Basic can handle the potential complexities that a Windows graphical interface hands to the programmer. Whereas graphical environments are often easier for the user than text-based environments, graphical environments offer tremendous challenges to program writers due to the windowed environment's multitask, multiwindow, multicontrol nature.

Topic 2: Visual Basic's Nature

FAST TRACK
Have you used Visual Basic before? If so, jump down to the next topic, "Compiling and P-Code", to learn about Visual Basic 5's new compilation option.

Actually, you've already been exposed to the nature of Visual Basic in the previous topic. To get you up and running as quickly as possible, however, we must spend the rest of the evening discussing today's benefits of Visual Basic and the nature of Visual Basic.

Overview

This topic explores the nature of Visual Basic programming more thoroughly. In this and the next topic for this hour, you'll learn the final introductory preliminaries that you need before diving into Windows programming. By the time you get to the next hour's lesson, you'll be ready to dive head first into a Visual Basic application.

Visual Basic Wears Many Hats

As you learned in the previous topic, Visual Basic isn't just a language, but is an applications development system. Although you'll have to master Visual Basic's language, you must also master Visual Basic's environment, including all the windows you saw in Figure 1.2.

Even though Visual Basic sometimes resembles a programming language when you're buried in the middle of code (code is another word for program instructions), Visual Basic resembles several software packages in that you use menu items, toolbars, and the graphical user interface to develop applications. You'll develop Windows programs that look and act just like the Windows programs you see all the time.


As you develop your Visual Basic applications, keep in mind that Windows programs follow a common pattern. Most Windows programs have File, Edit, and View menus; online help; and toolbars. You'll want to make your program as standard as possible, following as many of the other Windows products in style. You aren't plagiarizing but standardizing; in a Windows program, standardizing is so vital that Microsoft recommends that you match the company's own style with the Microsoft Office products' menus and toolbars. Microsoft realizes that the more that programmers standardize on common Windows components that exist in most programs, the less fear users will have and the more quickly users will adapt to the graphical environments.

Not a Windows Expert?

If you don't feel as though you're a master at the Windows 95 interface (the Windows 95 interface appears on different Microsoft operating systems, such as Windows NT and, of course, Windows 95), keep in mind that you don't have to be a Windows wizard to learn to program well with Visual Basic. However, you should be comfortable with using Windows, including these elements: window moving and resizing, menu bars, scroll bars, buttons, system menus, files, file names, icons, and dialog boxes (often called property sheets in Windows 95 terminology because of the multiple pages on many dialog boxes).


If you're unfamiliar with the Windows elements just mentioned, you should put aside Visual Basic for a short while and get acquainted with the Windows 95 interface. This book can't take time out of your Visual Basic night school tutorial to cover Windows fundamentals, but here are some great resources that should help you learn Windows 95 quickly: the user-friendly Using Windows 95 or the more comprehensive Special Edition Using Windows 95, both published by Que Corporation.

Visual Basic is Object-Oriented

Visual Basic is an object-oriented programming (OOP) language. OOP purists would cringe at that last sentence because Visual Basic doesn't contain a few of the advanced but needed requirements for a truly technically pure OOP language; nevertheless, Visual Basic is about as object-oriented as a language can get in many ways.

Object-oriented programming languages began springing up in the late 1980s as C++ and SmallTalk made a splash in the programming community. OOP programmers began rejoicing that, finally, their work backlog would be behind them because OOP would make programming virtually trouble-free. Today, the programming backlog in most computer departments is at least twice that of the late 1980s. Current OOP implementations are proving to be too difficult, and OOP proponents overestimated OOP's promise.

In many ways, Visual Basic, although not a pure OOP language, lends itself to the OOP spirit more so than many of the true OOP languages out there. Visual Basic instantly offers you a set of predefined objects that you'll recognize right away, such as command buttons that you can add to your program just by dragging them there with your mouse. Hooking these objects into your Visual Basic code is typically much simpler than doing the same from within a true OOP language such as C++.

Also, Visual Basic contains not only the standard Windows controls such as command buttons, but third-party developers are making new controls for you to plug into your own Visual Basic programs all the time. If you need to insert a spreadsheet or calendar in your Visual Basic program, you don't have to write the code; someone else has written a control that can embed itself as a spreadsheet right in the middle of your accounting program, and you can add a calendar feature to your Visual Basic program by dragging a calendar tool that's available in many flavors.

You can download many of these Visual Basic controls, or VBXs, from the Internet and other online services. Also, Visual Basic supports OLE Control Extensions (OCXs) and ActiveX controls, the newest controls that might help pave the way between desktops and Internet applications, among other functions.

The proliferation of Visual Basic control support makes Visual Basic popular. When you program, you don't want to be the only kid on the block who writes in your programming language. You want to use what everybody else uses so you can share skills, discuss problems, and trade code. Visual Basic is one of the world's best-selling programming languages out today, so you won't be alone when you begin writing Visual Basic programs.


Not only do you learn Visual Basic, but as a bonus, you'll also learn Visual Basic for Applications, a Visual Basic-like language that Microsoft has added to the Office 97 products. VBA is a close superset of Visual Basic, so you can use VBA to automate your Office 97 work immediately after mastering Visual Basic.

Event-driven programming requires that you understand events. In the world around you, an event might be the birth of a child, an Olympic meet, or a football championship. In addition to these once-in-a-lifetime events, an event can be an ordinary happening, such as a doorbell ringing or a car running out of gas. Events happen to you in life all the time, and you must make a decision every time an event occurs: Do you handle the event, ignore the event, or put off a response until the event occurs again? You handle events by prioritizing them and ranking your responses according to the events' purpose.

Computer events occur all the time as well. As in the real world, a computer event might be important or trivial. One of the most important parts of a Visual Basic program is deciding which events to respond to and specifying what that response should be.

When users run a Windows program, they do lots of things-press the keys, move the mouse, click screen icons, drag objects, cut, copy, paste, and so on. Your programs must run inside this multitasking environment. Some events occur for your program's sake (such as a user's answer to a question that your program asks), and some events occur that have nothing to do with your program (such as the user clicking the taskbar's Start button to start another program while yours is running).

Your programs must recognize what the user just did (that is, what event just occurred) and respond rationally to those. Given the variety of possible events, your program's job can be great just filtering through all the events that take place during execution.

Handling Events the Old-Fashioned Way

Before the advent of windowed environments, programs fell into one of two categories:

From its inception, the microcomputer has been interactive in nature with a single user and a single CPU (let's ignore parallel processing now, because the subject is still in its infancy and not prevalent among most microcomputer users quite yet). Therefore, most PC-based programs are online programs because there's always the user at the keyboard, and because batch processing is much less likely to take place except in rare situations.

Online text-based programs are designed to run with the user at the keyboard answering questions and responding to the program at the program's rigid prompting. These programs usually require users to select from a limited set of choices by pressing certain keys. Such systems often presented the user with a menu of choices such as these:

  1. Create an invoice
  2. Post a payment
  3. Send out late notices
  4. Exit the program

Although the user had limited control over the program's flow (the user might select any one of the four choices), the user couldn't control the method of interaction; the keyboard was the only input allowed. Also, the user couldn't change options and many times couldn't easily back up to previous menu items if desired, unless the program were specifically written to allow that.

Handling Events the Windows Way

Graphical user interfaces provide so much more functionality and usability than text-based systems. When color graphics first appeared on PCs, many software developers took the text-based menu and placed the menu on the graphics screen. The result was little changed from the older pre-visual programs.

As graphics boards became more powerful and as developers began integrating more and more graphics and less text onto program screens, it soon became clear that the old menu approach to program control couldn't work in the new environment because the keyboard by itself wasn't enough. Users had to have a way to manipulate graphic images as though they could grab an image on-screen and drag that image to another location. Although touch screens never really took off, the mouse made graphical interactions finally possible.

Unlike keyboard text, the mouse pointer isn't limited to the current input area on-screen. The user can move the mouse to any screen location and click on any object. Users liked pointing, drawing, clicking, and dragging, so Microsoft developed the Windows operating environment to give customers what they wanted. Of course, with the invention of the mouse, a new programming challenge was born: How would a program handle all the complicated interactions that the new graphics hardware allowed? The next topic section explains everything.

Example

Figure 1.3 shows a running Visual Basic program. The running program is the window titled "Bibliography Database Browser" in the middle of the screen. As you can see, you can execute Visual Basic programs from within the Visual Basic environment. The window is a complete stand-alone Windows application that you can run inside the Visual Basic environment and by itself in a stand-alone mode.

Figure 1.3

A Visual Basic program window often shows several controls.

Almost every element you see in the application's window is a control. You'll find text boxes (such as the ones labeled Name and Address), command buttons (such as the wide ones across the top labeled Titles, Authors, and Publishers), and even scrolling controls (labeled Publishers).

Visual Basic supplies many controls for you to use. Not only will you be able to develop Windows programs with common Windows controls such as the ones you see here, but from a variety of sources you can also purchase (many are free) and download several additional controls called custom controls. You can obtain custom controls that perform specific calculations (such as advanced scientific calculations), motion picture controls, dissolve graphic controls, grid controls, wild spin buttons, and list-selection controls that may not match anyone's idea of a Windows standard, but they do make for interesting applications.

The best thing about Visual Basic controls is that you don't have to do a lot to put them in your application. Simply install the control (if you want to use one that doesn't come with Visual Basic) and drag it onto your Visual Basic application's window. Throughout this book, you'll learn all about programming with Visual Basic controls. In the past, you would have to spend countless hours tediously programming the user interface for your programs. Today, with Visual Basic's power, you simply design the visual interface with your mouse.


After you place the control on your application, you'll see that you can change the control's behavior from its default behavior. For example, you can set special colors, adjust scroll bar scrolling areas, and modify the way the graphic-related controls display images.

Figure 1.4 shows a menu on a text-based computer screen. Now that you're used to Windows, such screens are rather boring, aren't they?

Figure 1.4

A traditional text menu is outdated today.

Figure 1.5 shows a simple example of how a Visual Basic programmer might design such a program. Although users still must control the order of the program execution with the choices on-screen, other controls appear on-screen that modify the way the program behaves. Without powerful and simple programming tools such as Visual Basic, the program behind such a screen would be complicated because of the large number of events that might happen in any order.

Figure 1.5

Menu selections take on new meaning in a Windows program.

Here are some of the possible events that users can trigger in Figure 1.5:

Next Step

Look at your favorite Windows programs to see how they're alike and how they vary. Although each program's primary purpose might be different (such as Microsoft Word and Excel), many Windows programs share commonality in menus and toolbars. Therefore, when you master one Windows program, you're that much ahead when you want to master another. Before the standard Windows environment came along, you would have to learn how to interact with several different kinds of programs; with Windows, you must learn only a single interface, and you know almost every other Windows program interface as well.

If you use Visual Basic to write a Windows program and the program must use a data file, you should add a File menu to your program's menu bar and put Open and Save commands on the File menu. (Where else would you want such file-related commands?) Although you might want to use the term Load instead of the Windows standard Open, resist the temptation to change the standard. Your users will appreciate that they don't have to learn a new interface to use your program.

Figure 1.6 reviews the event-driven nature of Windows. As you can see, one of many possible events can happen at any time. Windows filters the events and sends to the proper program the event that belongs with that program. It's then your program's job, if you've programmed the code correctly, to respond to that event or to ignore the event altogether.

Figure 1.6

Windows filters out some events before the events get to your program; your program must handle the rest.

Topic 3: Compiling and P-Code

After you write a program, your computer can't magically execute the program until you issue the order. This topic section explains how your computer takes the Visual Basic program you develop and converts that code into machine language (the only language your computer really understands at its core) behind the scenes with only the click of a button.

FAST TRACK
Want to know Visual Basic's most impressive feature? You'll appreciate version 5's new compile capability. Jump down to the section "Converting to an Executable Program" for more information.

Overview

No matter if you use Visual Basic to write small programs that calculate averages or gigantic applications that run corporate businesses, the overall program-development cycle looks something like this:

Although the rest of your night-school training will address each topic, this hour's lesson has spent considerable time explaining the differences between today's high-level languages that you write and the low-level machine language that your computer actually runs. The following sections briefly show why learning all that was vital to understanding the new version of Visual Basic.

Converting to an Executable Program

To run a Visual Basic program, you only need to click a toolbar button. Visual Basic does the rest of the work. Visual Basic then interactively runs your program. You'll notice a brief pause right after you run a program before you see the results. The Visual Basic system converts your application into an intermediate file called p-code. P-code (which stands for pseudocode) isn't machine language and isn't a high-level language. P-code is a Visual Basic-specific language that requires a runtime module (a collection of common routines that your programs access).

Although I could get really technical here, there's no reason to do so. The bottom line is that when you run a Visual Basic program, the Visual Basic system first converts your program to p-code, and then runs the p-code through a runtime module. The runtime module quickly converts the p-code-one line at a time during the execution-into machine language and executes those converted machine language instructions.

Until version 5.0, Visual Basic wasn't a compiled language but was an interpreted language by using the p-code system. An interpreted language isn't as efficient as a compiled language because of the on-the-fly interactive conversion of your program's p-code to machine language. Therefore, Visual Basic has been fairly efficient in the past due to a tight p-code conversion process and efficient runtime module, but Visual Basic hasn't been able to compete with speedy languages such as C++.

Starting with version 5.0, the Visual Basic system includes a true language compiler. When you compile a Visual Basic program before running the program (as opposed to simply running the program), the Visual Basic system takes the time to convert your entire application to a machine-language file called an executable file. You then can run the executable file, and the file runs much more quickly than an equivalent interactively converted p-code-based language.


Perhaps you've seen file listings with files that end in the .exe extension. Those files are usually fully converted machine-language programs that run about as efficiently as possible. Until version 5.0, you couldn't run a truly compiled Visual Basic program.


Just because compiled programs run faster doesn't mean that you'll forsake p-code now. The easiest way to see the results of the Visual Basic you write is to run them without first compiling them. Visual Basic automatically uses the p-code conversion process (you never have to specify p-code or even know it's there). During testing, executing your programs interactively provides the best means for testing and feedback.

Program Maintenance

As you learn to program, your night-school course would be doing you a disservice if you didn't learn about proper coding techniques as you learned the Visual Basic language and development system. There are many ways to write the same program. In today's changing world, you need to realize that the program you write today will probably need changing in the future. Therefore, when you write programs, you'll do so with future program maintenance, or updates, in mind.

As you learn topics throughout this course, you'll see ways to improve on techniques so that you'll be more easily able to maintain the program at a later date.

Example

Companies grow, merge, consolidate, and spinoff every day. If you write Visual Basic programs for a company, you'll be more valuable to that company if you write clear and concise code. For example, in Part II of this Visual Basic night-school course, you'll learn how to document your programs. Documentation resides inside a Visual Basic program to offer explanatory remarks about the code and explain what the code does at different places in the program.

Documentation isn't required. You can write any program and never document the program. Your computer won't care. Nevertheless, if someone is assigned to change your program later, the more you can document what you did inside the program, the more quickly the programmer can understand your code, make the updates, and move to another project. If you don't document your code, the programmer must spend extra time figuring out what much of your program is doing.

If you write programs for yourself and only for yourself, you still need to document! The program explanatory remarks that you add today will save you time in when you need to make a change to a complicated set of instructions a year later. Programs can become lengthy, and you won't always be able to remember why you did something; a simple remark here and there will aid in your program maintenance process.

Next Step

At this point, you're probably looking forward to the next hour's lesson, when you can create your very first Visual Basic program. You have the groundwork out of the way and understand much more about what's required as a programmer. You also understand what a program is really all about, and how the computer takes your Visual Basic program and converts it down into the low-level machine language code needed by your machine. Fortunately, the days of 1s and 0s are over; your computer and Visual Basic do all the work. Your dragging of a control onto your Visual Basic window might take you one-half second, whereas programming the same control from scratch in machine language might take a week or more. We've come a long way, baby!

Perhaps one of the best ways to round out this hour's lesson is to review some important terms related to programming and Visual Basic. Here is a list of terms that you should master as soon as possible. You've seen some of the terms already in this lesson; others you may have already known. Some might be new to you, but you'll face them shortly as you master the fundamentals of Visual Basic.

Summary

The first night is now over! You just received an hour's introduction not only to Visual Basic, but to the programming process in general. You're starting at Hour 1's lesson if you've never programmed a computer before. Although this hour's lesson has provided a summary of the programming history and process, you're much better prepared to tackle Visual Basic's specifics.

In Hour 2 you'll learn about Visual Basic's environment so that you can create your very first program.

Hour 1 Quiz

  1. True or false: Your computer can't understand high-level spoken languages such as English.
  2. What language is the only language a computer can actually understand?
  3. Why is Visual Basic more than just a computer language?
  4. Explain why Visual Basic is easier to learn than many other languages.
  5. Describe how Visual Basic adds visual impact to programs.
  6. Which process produces more efficient and faster programs-an interpreter or a compiler?
  7. Why should you conform to the standard Windows program interface as closely as possible when you write your own programs?
  8. True or false: Controls are commands that control the way your Visual Basic program behaves.
  9. True or false: Visual Basic has been a compiler and an interpreter since its inception.
  10. 10. What file-name extension do executable programs usually have?

Hour 1 Homework Exercises

  1. Start Visual Basic, press Enter when you see the dialog box, and display the File menu by clicking Alt+F. Does the menu look familiar to you? Many options on the menu match the sameFile menu you'll see in Microsoft Word and Excel. Visual Basic looks and feels like a true Windows program and contains most of the common Windows elements you're used to seeing. Stick to the same! Keep the program menus and toolbars on the programs you write similar to equivalent options on other programs' menus and toolbars. Don't add a Quit option to your program'sFile menu when most other Windows programs use Exit. (By the way, choose Exit from the File menu to close Visual Basic and return to Windows.)
  2. Start the Windows 95 or Windows NT Explorer and look at file extensions. You'll see lots that end with the .exe extension, meaning that those files are executable programs. Look in your Windows directory for files with names that look something like these: Vbrun300.dll, Vbrun400.dll, and Vbrun500.dll. These files are Visual Basic runtime modules that execute p-code generated from Visual Basic versions 3, 4, and 5, respectively. If you don't compile your application, you'll have to distribute the version 5 runtime module along with your program so your users can run the program's p-code. If you compile your programs, the programs will have no need for the runtime modules, and your programs will also execute more efficiently.

© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon and Schuster Company.