0% found this document useful (0 votes)
223 views

Introduction To Programming in Visual Basic

1584504412_1stChap
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views

Introduction To Programming in Visual Basic

1584504412_1stChap
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1

Introduction to Programming in Visual Basic

In This Chapter Introduction A Few Words about Computers In-House and Canned Programs Programming Languages Mainstream Support for the Visual Basic 6.0 Family Other Areas of Programming Structured Programming Techniques Procedural vs. Nonprocedural Object-Oriented Programming Why Object Oriented? Object-Oriented Programming Languages The Visual Studio 2005 Product Line

INTRODUCTION
We wish there was a magic potion that would make us learn programming. We read the materials and understand everything, but we cannot write programs. Programming instructors often hear such comments from students. No one is aware of any potion that will make you a programmer. Learning a programming language, however, is not that much different from learning a foreign language, such as French, Arabic, Persian, or Spanish. Many programmers believe programming is an art and that there are many ways to write codes. Perhaps that is why every programmer uses his own style to develop a program, but you may know by now, programs are a series of instructions telling the computer what to do. Once you become a programmer, you will be giving these instructions to the computer. Consider the following example:

Visual Basic 2005 by Practice

Say you want to learn chess. You can get a chess coach and read books to learn the rules and terms that are used in the game, but to play well, you need to practice and play with other chess players. Although everyone must follow the same rules, each individual applies them differently. That could be why players develop their own styles and savvy tactics. The same logic can be applied to programming. Learning a new programming language does not require studying the whole programming concept again. You can review examples, study screenshots, and, of course, practice, practice, and practice in order to learn the new programming language. In this book, you will be provided with a great deal of examples and syntax, so that not only can you study the concept but you can also review the codes and working programs. Many programming languages can be used to communicate with the computer. Some people prefer Visual Basic for a variety of reasons. This group thinks the visual interface of Visual Basic makes programming much easier and more fun to work with. The new changes in the Visual Basic programming language and the advancement of Dot NET technology have made this programming language one of a kind.

A FEW WORDS ABOUT COMPUTERS


We do not know when man decided to use numbers for calculations. There is no real proof of what man was using to add and deduct numbers. However, history remembers the abacus as the first known tool that man used to calculate numbers. The abacus is a manual calculator that allows the users to add, subtract, divide, and multiply, similar to the pocket calculator many of us use today. With some practice you can operate the abacus as fast as you operate a calculator. Computer history credits Charles Babbage as fathering the concept behind modern computers. Computers are designed to process raw input and generate meaningful information by calculating, organizing, formatting, and generating reports. Most computers consist of the following components: Hardware Software Hardware Hardware is the physical components of the computer such as input devices (keyboard, mouse, microphone, camera, etc.), output devices (printer, monitor, speaker,

Introduction to Programming in Visual Basic

etc.), storage devices (hard drive, memory, floppy disks, CDs, etc.), and the central processing unit (CPU). The CPU is known as the brain of the computer. All processes are done by CPU. Software Software is collections of computer instructions or programs. Programs are written by programmers and instruct the computer what to do and how to do it. We can divide software into two major categories: System Software Application software
System Software

System software is designed to manage computer resources. Consider system software a host that serves all invited guests. System software interacts with the users and the computer. System software includes operating systems, such as Windows, which most people are familiar with. In addition to operating systems, there are utility programs that go under the system software umbrella as well. Utility programs may or may not be included with the operating systems, but they are there to assist you with your day-to-day tasks. An example of a utility program would be a backup program that allows you to copy and save your files for later use.
Application Software

The other category that plays an important role for computer users is the application programs, also known as end user software. These are designed to do specific tasks.

IN-HOUSE AND CANNED PROGRAMS


Application software can also be divided into canned programs and in-house programs. Canned, or out-of-box, programs are software packages such as word processing, database, spreadsheet, and presentation packages that can be purchased from software stores. These programs are mass produced and made available to everyone. No matter where you buy them, all of those programs function in the same way. As a user, you need to adopt their options. In-house programs are designed by in-house programmers to meet a companys specific needs. Application programs can be written for business and scientific needs (see Figure 1.1).

Visual Basic 2005 by Practice

FIGURE 1.1 Hardware and software organization.

PROGRAMMING LANGUAGES
In order to communicate with computers, we need to speak a language that computers can understand. Like verbal languages, programming languages have their own grammar, punctuation, and vocabulary. Programming languages have gone through many changes and evolved to be more understandable to humans. Lets look at the history of programming languages. Low-Level Programming Language Machine language is the only language computers can understand. The instruction is given to computers in binary digits, or a combination of 0s and 1s. Bi in Latin means two, referring to two states of ON and OFF that 0 and 1 represent. Machine language is the lowest-level language and is understood by machines only. It would be very hard for a human to program in binary codes, if not impossible. The smallest units of data is called bits, which stands for binary digits. Machine language is hardware specific, and each processor needs its own machine language. The code also contains the instructions to load, store, move, halt, and other functions needed to perform arithmetic operations.

Introduction to Programming in Visual Basic

The lack of standards among machine languages used by different computer manufactures prevented computers from exchanging information. Later, in the 1960s, ASCII codes were developed to standardize machine language codes to allow computers designed by different manufactures to communicate with each other and exchange data. ASCII, which stands for American Standard Code for Information Interchange, was the product of this effort. The ASCII character set represents 128 characters that are found on most standard keyboards. These 128 characters consist of numbers, letters, and special characters, such as commas, parentheses, and dollar signs. This character set ranges from 0 to 127. This is known as a 7-bit character set that can represent the first 128 characters. Since most computers have additional special character sets beyond the first 128 characters, the 8-bit or byte ASCII character set known as Extended ASCII was developed later on. Although ASCII codes provided some standards for the exchange of information between computers, the codes remained semistandard. Table 1.1 shows the ASCII and binary codes for a few characters:
TABLE 1.1 ASCII and Binary Codes ASCII 065 066 067 068 069 070 071 072 073 074 Binary 01000001 01000010 01000011 01000100 01000101 01000110 01000111 01001000 01001001 01001010 Character A B C D E F G H I J

As you can see, the ASCII code for the capital character A is 65. You can find most of the ASCII codes and characters in Appendix A. If we want to see how the word hi is represented in binary and ASCII, it will be as shown in Table 2.1.

Visual Basic 2005 by Practice

TABLE 1.2 Examples of ASCII and Binary H ASCII Binary 72 01001000 I 73 01001001

We can use ASCII codes in our Visual Basic Programs to validate the input. You will learn how to do it in later chapters. Second Generation of Low-Level Programming Language As stated above, machine language is designed for a machine and is not readable by humans, but humans are the ones who must write instructions for the computers. To make it possible for humans to read and write computer instructions, assembly language was developed. Assembly language uses a translator called an assembler to translate programmers instructions to machine language. The structure of assembly language is very similar to machine language. However, programmers can use symbols instead of the raw numbers, which makes programming easier. Assembly language is also known as a low-level programming language because like machine language, it is designed to be used by machines. Like machine language, assembly language is hardware specific and it varies from CPU to CPU. There is no machine language that can be used on all processors. A simple example of an assembly language instruction would be:
LOAD R5 #20 ADD R2 R3 #10

In the first instruction, you load the value 20 into register number 5. In the second instruction, you add 10 to the value that is stored in register number 3 and store the result in register number 2. Each instruction will be abbreviated into a three-letter name known as its mnemonic. For instance, JMP would represent the JUMP instruction. High-Level Programming Languages Although assembly language is easier to use than machine language, it is still hard to learn. As we learned, computers can understand only machine language, and we know humans cannot use machine language directly to comfortably instruct the computer to perform tasks. As a result, the high-level programming languages were developed. The grammar (syntax) of some of the high-level programming languages is close to English, so programmers can learn and use them faster and more easily.

Introduction to Programming in Visual Basic

Under the category of high-level programming languages, you will find many programming languages that were developed to solve specific problems. Unlike machine language and assembly language, high-level programming languages are designed to interact with humans, not machines. The high-level programming languages need to be translated into machine language in order to be understood by computers, which can be accomplished by using compilers and interpreters.
Compilers and Interpreters

A compiler is a computer program that translates the entire program into object codes. You can execute this object code without the need to compile it again. Most executable programs that you receive and execute are object codes, and you do not need the source code to run these applications. Once they are compiled, they will run as many times as you need. If you need to modify the program, you can go back to your source codes, make the changes, recompile it, and generate a new object code (executable program). Since the program is already compiled, running the executable program is much faster than compiling and running the program simultaneously. During compilation, syntax errors will be identified, and the program will not compile if errors are found. The compiler will break down your errors into unrecoverable errors and warnings. Some programs can still execute with less serious warnings. Consider a compiler as your English teacher. When you submit your essay, your teacher reviews it for spelling, punctuation, and grammar. At the end your teacher will count the number of errors and assign a grade to you. This is very similar to what a compiler does. An interpreter translates one instruction at a time, rather than translating the whole program. Object codes will not be generated when interpreters are used. As a result, every time you execute this program, it will go through the translation process again. This will cause it to be slower than programs that use compilers. Because interpreters translate one instruction at a time, some errors can be identified once the user hits the enter key. This interaction will help beginners find their errors without waiting until the entire program is translated. Interpreters are not as efficient as compilers and are slower. Many versions of BASIC use compilers as well. Lets look at some of the high-level programming languages: FORTRAN BASIC COBOL C Pascal C++ Visual C++ JAVA

Visual Basic 2005 by Practice

Visual J++ VISUAL BASIC Visual C#


FORTRAN

FORTRAN, which is an acronym for formula translator, is the oldest third-generation programming language. It was developed in the 1950s as a scientific programming language designed to solve mathematical problems. FORTRAN uses a compiler to translate English, high-level third-generation source codes into a language the computer can understand. The programmer needs to follow the syntax of the language and not worry about the way the instructions are read by the machine. The compiler will also give hints for possible errors in the program and will not compile the source code (your program) if it contains errors. FORTRAN is still around, and new versions of it are available. Lets look at one simple code:
print*,'Enter a number' read*,num1 print*,'The number you entered was ',num1

In the above statement, the users input will be stored in the num1 variable and will be displayed back to the user.
COBOL

COBOL is an acronym for Common Business Oriented Language and was designed to solve business problems. COBOL was developed in the 1960s and was intended to be an easy-to-understand programming language, and most application programs designed in COBOL are still in use. Inventory, payroll, and accounting applications are among programs that COBOL is good at. COBOL is a wordy language, and you need to spell out many statements to make the program work. This makes COBOL more readable. COBOL can process a large number of input files, which is a convenient feature for business applications. Like FORTRAN, COBOL has its own compiler. COBOL has gone through many changes, and newer versions are designed for PCs. COBOL has four specific divisions that need to be ordered in sequence, and this hierarchical structure has made the program a very dominant programming language (see Figure 1.2). As you can see in Figure 1.2, each COBOL program consists of up to four divisions. Two of them, the identification division and the procedure division, are required to execute the program. Each division may have several sections, and each section may be composed of one or more paragraphs. Each paragraph may have one or more sentences, and so on. These divisions must appear in the following order:

Introduction to Programming in Visual Basic

FIGURE 1.2 COBOL hierarchy structure.

Identification division Environment division Data division Procedure division The identification division is designed to collect information about the program:
PROGRAM-ID. AUTHOR. DATE-WRITTEN. DATA-COMPILED. SECURITY. INSTALLATION. MY-FIRST-PROGRAM. Mike-Mostafavi. September-2005. October-2005. None. Testing.

Not all of the items within the divisions are required, but most programmers supply all the information for documentation purposes. For instance, within the identification division, only Program-ID is required. COBOL statements are not case sensitive.

10

Visual Basic 2005 by Practice

The environment division is designed to collect information about the source of input/output and other external devises to centralize all resources in one area. Here is an example of that:
CONFIGURATION SECTION. SOURCE-COMPUTER. mine. OBJECT-COMPUTER. yours. INPUT-OUTPUT-SECTION. FILE-CONTROL.

The data division contains information about data items that will be processed within the program. It may have sections such as file section, working-storage section, linkage section, and report section. The procedure division is designed to hold codes to process data that will be used within the program. It supplies the necessary codes to produce the required information and reports. As stated before, COBOL has English-type statements that can be understood by nonprogrammers as well. Here is an example:
multiply pay-rate by hours-worked giving gross-pay

BASIC

BASIC is another high-level programming language developed in the 1960s. BASIC is an acronym for Beginners All-purpose Symbolic Instruction Code. BASIC is a general-purpose programming language but has been widely used in the business world. BASIC was developed at Dartmouth College to teach students the concept of programming. Compared to some other programming languages, BASIC is the easiest to learn. Newer versions of BASIC languages, such as Visual Basic, share syntax with the old version. Here is an example for a BASIC program:
PRINT INPUT PRINT INPUT IF P$ PRINT "Enter Your Name"; N$ "Enter your password" P$ <>"BASIC" THEN "Wrong Password"

BASIC was using an interpreter instead of a compiler, which have several differences.
PASCAL

Pascal is another scientific high-level programming language that was designed in the 1970s. Pascal was named after the French mathematician Blaise Pascal. Pascal was very popular before programmers became familiar with C language. It is capable of doing scientific applications and is still used by some programmers. Pascal was

Introduction to Programming in Visual Basic

11

famous for being a very structured programming language, and some educators use it to teach structured programming techniques. Here is a portion of a Pascal code:
Writeln(Make Your Choice); writeln; writeln("English"); writeln("Spanish'); writeln('"Persian'); writeln; Readln(menuchoice);

C is another high-level programming language, which was developed in the 1970s in the Bell Laboratory. C is the third try after two other similar programming languages, BCPL (Basic Combined Programming Language) and B, did not meet expectations. C was used to write the UNIX Operating System and works well within that operating system. C, which is easier than assembly language but harder than other third-generation programming languages, is still widely used by a large number of programmers.
{ /* The following will display a message to the user */ printf("Display My Message\n"); return 0; }

Visual Basic, Early Versions

Although Visual Basic was not the first programming language designed to develop a Windows application, it is perhaps the first Visual programming environment that has been introduced to programmers. Since Visual Basic syntax is based on the early version of BASIC, we need to expand our discussion of it and explore its background a little deeper. As we know, the first generation of BASIC programming language used an interpreter. BASIC used an interpreter and ran on Mainframe computers, and students could use it at their campus computer labs. The same method was used when the DOS Operating System was developed for personal computers. BASIC-A was a version that was developed for IBM PCs. Later on, Microsoft developed GW-BASIC, which was bundled with MS-DOS Operating System for IBM compatible machines. GW-BASIC was named after Greg Whitten, a former Microsoft employee. GW-BASIC was replaced by QBASIC (stands for Quick Beginners All-purpose Symbolic Instruction Codes). QBASIC accompanied the Windows 95 Operating System package. QBASIC is different from QuickBASIC, which was sold separately from the bundled versions of BASIC. QuickBASIC was a commercial package that had more programming capabilities and used a compiler rather than an interpreter.

12

Visual Basic 2005 by Practice

Along with the development of Windows 3.0 in 1991, the first official version of Visual Basic was developed. Visual Basics drag and drop features that ran under the Windows operating systems created an easier development environment. Visual Basic 2.0 was released in 1992 and introduced new features and better performance. Version 3.0 hit the market in 1993, with new features including a database engine to connect to databases. It was also offered in both professional and standard versions. Version 4.0 was released in 1995, and one of its top new features was the ability to create custom-made controls and classes. In 1997, Visual Basic 5.0 was introduced, offering much faster performance and ActiveX controls. Visual Basic 6.0 was released in 1998 and offered new features, including Webbased applications. This version was offered as a studio package that included other applications, such as Visual C++, Visual FoxPro, Visual Interdev, and Visual J++. Although Visual Basic 6.0 became a very popular programming language, it was often criticized for not being a fully object-oriented language. Many people considered Visual Basic 6.0 an event-driven programming language, not an objectoriented programming language. The .NET version of this programming language has corrected all of the object-oriented shortcomings found in previous versions of Visual Basic.

MAINSTREAM SUPPORT FOR THE VISUAL BASIC 6.0 FAMILY


Visual Basic 6.0 is still used around the word, but Microsoft has stopped its mainstream support for this product family. The Visual Basic 6.0 family of products includes the standard, professional, and enterprise editions of Visual Basic 6.0. According to Microsoft, the mainstream support was in effect for six years after the software was available to the general public. Since Visual Basic 6.0 was released in 1999, mainstream support ended March 31, 2005.

OTHER AREAS OF PROGRAMMING


You should look into other high-level programming languages in order to be familiar with their nature. ADA and PL/1 are among many others that need recognition. The Internet With the evolution of the Internet, the need for other programming languages became unavoidable. The traditional stand-alone programming paradigms couldnt

Introduction to Programming in Visual Basic

13

meet the demands of this new communication media. As a result, a new language was developed to facilitate communications among users. The developers of this new language intended to keep it simple and easy to learn. HTML, which stands for Hyper Text Markup Language, was developed in the 1990s. This language uses tags for specific instructions, which are translated into meaningful Web pages by special software called browsers. Here is a short sample of HTML code:
<html> <head> <title>Visual Basic 2005</title> </head> <body> <h2>Visual Basic 2005</h2> <h3>In this book we learn how to program</h3> <p>Here are topics we will cover:</p> <ul> <li>History of programming languages</li> <li>Visual Basic</li> <li>other topics</li> </ul> </body> </html>

Is HTML a programming language? This has been the center of many debates. According to a lot of experts, HTML is a markup language but not a programming language. Whether HTML is a markup language or a programming language is not important. HTML is the backbone of the Web pages we visit every day. JavaScript Since we talked about JAVA and HTML, we should talk about another language that most Web developers use in their day-to-day design. Although JAVA and JavaScript share a lot of similarities in both name and structures, they are two different programming languages. JAVA is a stand-alone programming language that can run on any platform. JavaScript, on the other hand, is a scripting language that must be included within HTML codes and be processed by browsers. JavaScript was developed by Netscape in the 1990s to allow Web developers to add more interactive features to their Web sites. Here is a short sample code for JavaScript:
<HTML> <HEAD> <TITLE>My JavaScript page</TITLE> <SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT"> alert("Welcome to my JavaScript page!") </SCRIPT> </HEAD> </BODY> </HTML>

14

Visual Basic 2005 by Practice

Most of the newer browsers support JavaScript. Programmers use JavaScript to enhance their Web pages. Password protection and animations are among the features JavaScript offers that appeals to programmers. VBScript VBScript is a Microsoft Scripting language that is based on the Visual Basic programming language and shares some similarities. Like JavaScript, VBScript was designed to help Web developers add more interactive functions to their Web pages. VBScripts can be viewed by the Internet Explorer browser, but Netscape doesnt support it. Due to such limitations, VBScript is not as commonly used as JavaScript. Here is a short sample of VBScript that is included within HTML codes:
<html> <head> <TITLE>This is a sample</TITLE> <SCRIPT LANGUAGE="VBScript"> MsgBox "This is Visual Basic 2005 Textbook" </SCRIPT> </head> <body> <h2> Table of Content :</h2> </body> </html>

STRUCTURED PROGRAMMING TECHNIQUES


You should also be familiar with another common term that is used in the programming environment: structured programming techniques. You may hear structured programming techniques and top-down design used interchangeably. With this technique, you start writing the most general part of your program first and test it. Then you write codes for the most specific part of your program. With this hierarchical programming technique, modules are used to break down the program into smaller sections. These modules normally have single entry and single exit points. The control is normally passed downward. An example would be creating a main menu for your program that gives the options of Add, Delete, Update, and Undo to your users. You create the menu by using the stub testing technique and make sure that it branches out to the routine that you want. At this time, you havent written the codes for the Add, Delete, Update, and Undo sections of your program, but you know that the control is passed to that section. Stub testing refers to an empty section that returns feedback to the programmer. For example, you can have a message box within the Add section of your program read Add routine was accessed. After this test, you start writing codes for these tested empty routines.

Introduction to Programming in Visual Basic

15

The opposite of top down is the bottom up technique, which requires you to start with the most specific part of your program and then go to the most general parts. For example, you could write codes for the Add, Delete, Update, and Undo sections first and then create a menu to connect these sections together. The structured programming techniques can be applied to procedural programming languages such as ADA, Pascal, FORTRAN, COBOL, and C but are not as applicable to object-oriented programming techniques these days.

PROCEDURAL VS. NONPROCEDURAL


If you do a Web search or review programming textbooks, you will notice that some Web sites and authors divide programming languages into five generations: first generation (machine language), second generation (assembly language), third generation (FORTRAN, BASIC, COBOL, etc.), fourth generation (Focus, Fourth, etc.), and fifth generation (Prolog). You will also notice that some of them list objectoriented programming languages as fourth-generation programming languages. We cannot find solid and unified definitions for the generations of programming languages but we can see some agreement about topics mentioned in this section. Further research will show you other interesting topics in the area of programming languages: Procedural programming languages Nonprocedural programming languages Older high-level programming languages are procedural languages. Under this category, programmers need to follow specific orders to write their codes. The program consists of at least one main routine and may have several subroutines. Under this category, you need to tell the computer what to do and how to do it. Examples of programming languages that are listed under procedural are: FORTRAN COBOL PASCAL BASIC C Nonprocedural programming languages, however, do not follow a specific sequence. Object-oriented programming languages such as Visual Basic.NET, which are nonprocedural in nature, provide an environment that puts the user in control. The user, in turn, can choose the sequence in which the program should be executed.

16

Visual Basic 2005 by Practice

Object-oriented programming languages that fall under the nonprocedural category continue to get more attention. The efficiency, reusability, and performance of object-oriented programming has convinced many programmers to move to that direction.

OBJECT-ORIENTED PROGRAMMING
If you have taken programming classes recently, you have most likely heard the phrase object-oriented programming from your teachers or textbooks. Since Visual Basic 2005 supports object-oriented programming, lets get familiar with some of the terms that are associated with this concept. Object Object-oriented programming refers to a program that uses objects, such as TextBoxes, Buttons, and Labels. In real life, consider an object such as your cell phone. Your cell phone, as a device, can be represented to the computer by an object. The model, color, size, price, frequency, and other features will be used to represent your cell phone. We use tools or controls to create these objects. Attributes or Properties We use attributes or properties to identify objects. Each object must have a name, color, size, and other characteristics. Each object has several attributes or properties. In our cell phone example, model, color, size, price, frequency, and other characteristics that are used to describe a cell phone are attributes or properties of the cell phone. We use properties to define objects. A TextBox, for example, has properties such as Name, BackColor, Size, Font, BorderStyle, and Text. We can use these properties to create TextBoxes according to our needs. Methods Each object is designed to perform specific operations or actions. These actions are called methods. For our cell phone example, End, Send, and Clear are considered methods. Objects have standard methods that can be used by the programmers to do specific tasks. For example, an object such as a button can have methods such as Refresh, Hide, Show, Focus, and so on. In Visual Basic, the programmer can reference the objects methods by using the objects name and the name of the method separated by a dot: Object.Method. Lets say the object is a button and we have renamed this object processButton. This can be demonstrated by the following examples:

Introduction to Programming in Visual Basic

17

processButton.Hide processButton.Show processButton.Focus processButton.Refresh

Events Objects can react to the users input according to the event that the developer has programmed for. For example, Click is one of Buttons many events that can be used by the user to perform a task. Lets say you see a button that reads Exit Program. Once you push this button, the program will be terminated. The programmer has placed a set of codes within the event subprocedure of the Exit button to fulfill your request. Other events for the Button and other objects will be discussed later on. Classes and Instances One of the important elements of object-oriented programming is class. Think of a class as a rubber stamp you have made for your name, address, and zip code. Any time you use this rubber stamp, an identical instance of the information will be printed on paper. The class is like a rubber stamp or template that maintains the properties and methods needed for each object. In our cell phone example, Folding Phones could be defined as a class that represents similar cell phones. A Button class maintains all properties and methods needed to create a button. Every time you use a tool, such as a Button class to create an object, you make an instance of that class. An instance is like a photocopy of the original that can be reproduced x number of times. Encapsulation The objects communicate with the users through the interface that is designed by the programmer. The user shouldnt know what properties of the object have been used or what kind of processing takes place behind the object. Encapsulation is used to hide the properties and methods of the object. Based on the service that the encapsulation provides, the user will communicate with the objects only through the interface. This allows the programmers to change the data source with no effects on the users. Inheritance Inheritance is another term you should be familiar with. Consider the cell phone example again. All cell phones of a particular make, model, and year will inherit the same characteristics. Now, we will apply this term to programming. Lets say you have created a Form with specific parameters, such as specific background color, size,

18

Visual Basic 2005 by Practice

and so on. You would like to make some changes to this Form but you want to keep the original intact. In this case, you create another class from the Form class you created before, and the new class inherits all properties and methods of the original. This will enable you to reuse an existing class. The terms parent class and child class refer to the original and duplicated classes. Polymorphism There is one more term to understand before moving on: polymorphism. Poly in Greek means many, and morph means forms. This means different objects that have methods and properties with the same name react to them differently. For example, two different objects could have similar methods. Although the methods names are the same, they function differently when you execute the program. Heres an example. Say you plan to wake up at 6:00 a.m. every day. You have three ways to accomplish this task: an alarm clock, a clock radio, or having your spouse wake you up. The alarm clock will ring, the clock radio will sing, and your spouse will kick. As you can see, each will respond differently to your request. It is also possible that one method within a class has different names and behaves differently. The classic programming example used by many teachers is the drawing method that is available for the Shape class. By using the drawing method you can draw different shapes such as triangles, squares, and so on. Reading these concepts without hands-on practice is like reading a driving manual without a car. Just be patient, and most of these terms will make sense to you as we proceed.

WHY OBJECT-ORIENTED?
As stated before, the industry is moving toward object-oriented design. Although the move was slow in the beginning, it has received positive acceptance in recent years. We are not going to get into a debate to prove which of these, procedural or object oriented, is better. There are valid arguments from both sides. However, with the popularity of object-oriented programming languages such as JAVA, C++, C#.NET, and Visual Basic.NET, object-oriented programming languages prove to be faster to learn and easier to maintain. In addition, object-oriented programming languages provide a way to break large and sometimes difficult-to-manage programming projects into smaller modules that can be managed easily. Encapsulation hides technical and complex details from the users and allows the programmers to make changes to the data and code without affecting the users. The author is a firm believer of object-oriented design and uses it professionally for programming projects.

Introduction to Programming in Visual Basic

19

OBJECT-ORIENTED PROGRAMMING LANGUAGES


Now that you are familiar with object-oriented terms, we will introduce you to some object-oriented programming languages. C++ C++, which is an extension of the C programming language, was also developed at the Bell laboratory. C++ was introduced in the 1980s and became a popular objectoriented programming language. The distinct features of the C++ language are class- and object-oriented features that were added to it. C++ was also designed for the UNIX operating system. It is used by many developers and is taught at many schools. Here is a portion of the C++ source code:
#include<myfile.h> int main() { int A; cout << "Enter a Number "; cin >> A; cout << "The number you entered was:" << A; return 0; }

In the above code, #Include is a header statement that is equivalent to Import in other programming languages. It is used to import functions from other files. Visual C++ After the introduction of C++ programming, Microsoft introduced a new version of C++ that ran on Windows operating systems. This new version of C++ is called Visual C++. Visual C++ is added to the Integrated Development Environment (IDE) of the Visual Studio family and supports a 32-bit Windows application and has its own library. This library, which is designed to help programmers, is called Microsoft Foundation Classes (MFC). The new Visual Studio.NET 2003 and Visual Studio.NET 2005 include Visual C++ as Visual C++.NET. JAVA JAVA, which is another object-oriented high-level language, was introduced in the 1990s. Unlike other programming languages, JAVA is not dependant on any specific platform and can run on any operating system and platform. JAVA was developed by Sun Microsystems, but Microsoft has a different version of it included in Visual Studio packages. Sun Microsystems JAVA became popular for the new features it offered for the Web. It can run as a stand-alone program or use a browser to execute JAVA Applets. Here is a small portion of JAVA code:

20

Visual Basic 2005 by Practice

System.out.print ("Enter Your Name"); System.out.flush(); Username=system.in.read(); System.out.println("Your Name was: " + username);

Visual J++ Visual J++ is the Microsoft version of JAVA. This graphical user interface (GUI) development tool helps users develop JAVA programs using the graphical editor and debugger. Microsofts Visual Studio series, which is a collection of several integrated languages, includes Visual J++ as well. Visual J++ allows developers to enhance their development skills by using a GUI. Visual C#.NET C# (pronounced C Sharp) is the newest object-oriented programming language added to the Visual Studio.NET IDE family. Visual C# has incorporated the ease of the GUI that Visual Basic offers and the capabilities of other programming languages such as C and JAVA. Most JAVA programmers are attracted to C# for these two futures. Here is a sample code for it:
public class HelloWorld { public static void Main() { System.Console.WriteLine("Hello World!"); } }

Visual J#.NET This is the replacement for J++ offered by a previous version of Visual Studio. Visual C#.NET was designed by Microsoft to facilitate JAVA programmers transition into the Visual Studio.NET integrated environment. Although Visual J# (pronounced J Sharp) is not fully compatible with Suns JAVA, it has attracted a lot of attention. The syntax of Visual C#.NET resembles Visual J# .NET. Visual Studio.NET In 2001, Visual Basic.NET, which was included in the new Visual Studio.NET suite, came with new features that were not offered by any of its predecessors. Unlike other versions of Visual Basic, the .NET version provided a complete object-oriented environment. Visual Basic.NET, Visual C++.NET, Visual C#.NET, and Visual J#.NET were all part of the same IDE. This new version also simplified the development of Web-based applications by providing support for ASP.NET. However, Visual Studio.NET is not backward compatible, so projects that were developed using the previous versions of Visual Studio cannot run on the new version.

Introduction to Programming in Visual Basic

21

Visual Studio.NET 2003 was released in 2003 and included wireless support for the first time. The performance of VS.NET has improved in many areas.

THE VISUAL STUDIO 2005 PRODUCT LINE


Visual Studio 2005 products are offered at several levels: Team Systems, Professional, Standard, and Express Edition. Express Edition Microsoft has released six Express product lines that consist of Visual Basic 2005, Visual C++ 2005, Visual C# 2005, Visual Web Developer 2005, Visual J# 2005, and SQL Server 2005. This edition, which is relatively light and inexpensive, is designed for beginners, hobbyists, students, and novices who want to learn Windows and Web programming. Standard Edition Visual Studio Standard edition is a development tool with the simplicity of the Express edition but it is designed to be used by small businesses. This edition includes tools to design Web applications. Professional Edition Professional developers will have two options: Visual Studio 2005 Professional edition, and Visual Studio 2005 Tools for the Microsoft Office System. Each edition has comprehensive tools that help professionals develop Windows or Web applications. Team Edition This edition is designed to foster a productive and professional team environment, improve communication among the team members who work on business projects, and reduce the complexity of the solutions.

SUMMARY
In this chapter, we covered the background of programming languages and provided you with simple codes in each programming language. We also discussed the differences between procedural and nonprocedural programming languages and structured techniques, explained the concept of object-oriented design and provided you with all terms used in object-oriented programming.

22 Next

Visual Basic 2005 by Practice

In Chapter Two, we will discuss Visual Basic 2005s IDE and cover some of its new features. You will learn about the Properties window, the Solution Explorer, Document window, Toolbox, tool bar and Menu bar. You will also become familiar with the IntelliSense feature, Code Snippets, and many other terms.

DISCUSSION QUESTIONS
1. What are the differences between procedural and nonprocedural programming languages? 2. Why do we call machine language a low-level programming language? Please explain. 3. What was the role of BASIC programming language in the development of Visual Basic? 4. Explain class and method in the context of object-oriented programming and provide examples. 5. Is Visual Basic an event-oriented programming language? Why? Exercise Visit the publishers Web site and right-click on any page. Select the View Source option and review the codes. Try to locate HTML tags and JavaScript codes. List your findings and briefly explain them. Key Terms Class, Parent class, and Child class Encapsulation Events Event subprocedure Inheritance Instance Methods Objects Object-oriented programming Polymorphism Properties

You might also like