Home -> Programming for Beginners

Programming for Beginners

Why Visual BASIC?

Visual BASIC is the language of choice for several reasons. BASIC was one of two languages developed to teach programming. The other language is Pascal. BASIC came first and suffered from several problems that caused Pascal to be created. Pascal is a more structured language. We will speak more of structured programming later. Visual BASIC is an adaptation of BASIC which corrects the most glaring flaws of earlier forms of BASIC and provides development tools to create software which uses a Graphical User Interface (GUI). The GUI was created by Xerox in their Palo Alto research facility between 1969 and 1980 and debuted in the marketplace in the Xerox Star. Apple Computer first introduced it in their Lisa in 1983, and then in the Macintosh in 1984. In the late 1980's Microsoft began developing a GUI operating system for PC or Wintel machines. Microsoft added a GUI look to DOS 3 by using character graphics to simulate the GUI. Their first GUI operating system was Windows 95 released in 1995. Since 1995, all modern consumer software follow the GUI interface principles. There is still a fairly wide variance in the quality of GUI systems and software available in the marketplace. VB is pretty faithful to GUI principles and as a programming language, it has a shorter learning curve than some of the other languages such as C, C++ or Java while maintaining significant power. In the new .NET environment, VB can do almost anything the other languages can do. If you are going to learn to program as an interest or hobby, you might as well learn a language which is easier to learn, allows your programs to be easy to use, and looks as good as professionally created programs. Of course it takes a lot of polishing for them to look, act and feel that good.

There are two more big advantagse of using VB in its current incarnation. Visual BASIC 2005has two versions. The full developer version still costs a fair chunk of change, but Microsoft has chosen to make its VB 2005 Express edition available FREE to everyone from their own web site. This easy and inexpensive availability makes it a stand out choice for the amateur programmer who is working on the Wintel platform. With the extensive library of routines, objects and controls available in VB, the amateur can create great looking programs in a fraction of the time it used to take. The second advantage is that with VB 2005 VB became a fully object oriented language. VB does not require you to use objects, but using them reduces development time significantly!

It is only possible for a relative beginner to create this modern software because large amounts of code have already been written and come as part of the VB package! In effect we stand on the shoulders of many professional programmers. We have only to create the code which our particular program requires. In my opinion this makes VB the stand out c hoice for amateur programmers! If you do not already have VB installed on your machine, the next section will help you do so.

Installing Visual BASIC

These pages are here as support for people who want to begin to learn how to program in Visual Basic (VB). Microsoft makes a free version of Visual Basic 2005 - called Express, available through its web site. To get a copy, go to the Microsoft Visual BASIC 2005 page and click the download button for Visual BASIC 2205 Express Edition. Follow the instructions on the download page to download the software and install it on your PC. I also suggest you download Service Pack 1 as well since it provides fixes for the basic package. As part of the main package, the installer will also install VB Framework 2 which is required for executing compiled VB programs. The instructions on the web page will walk you through the decisions you need to make as you do it. Before proceeding to try any of the programs listed in these pages, you must to complete the installation of VB 2005 Express.

The latest version is now VB 2010. You can download either VB Express 2008 or VB Express from this page. Select the version you would like to use and click on he link just below the Downloads title. Remember you are after the Express version. Visual Basic is a language and Visual Studio is a program that will allow you to use several different languages with the same editor.

Using the Visual Studio Editor

When you launch VB, it opens up the Visual Studio Editor. You then have the option of opening an existing file or project, or creating a new project. If you double-click an existing *.sln file then the editor is opened, and the project opens inside the editor. (Note: * is a wildcard character and represents any number of characters: in other words any file ending in .sln, is a VB project file. More on project files and their organization later.)

Fundamental to effectively using the Editor, is the necessity of understanding that this program is always working in one of three modes. The program is in Design Time while we are creating forms, placing controls on the forms and writing code for our project. When we want to test the program we start run it. From this point on we are in Run Time until we stop execution of the program, or the program fails. If we stop execution of the program, then we return to Design Time and can continue building our project. However, if our program crashes, the editor goes into Break Time. This is where most people run into trouble because they do not realize that the program is still running but now paused. To find out is your program is in Break mode look on the toolbar for a bule square (See the graphic below.)

When the execution of the program is paused, we can look at what is going on inside the program at the moment when it crashed. While in Break Mode we cannot alter the code of the program, but can perform investigative work to understand why the program crashed. I will say more about using Break Time to debug our programs later on. Visual Studio has powerful debugging tools which can greatly assist in hunting down problems in our programs.

Introduction to Programming

To learn to program is to learn to read and write a new language. We do not learn the computer's language, which is Machine Language, also called Binary Language. The only symbols in Machine Language are 0 and 1. Groups of 0s and 1s make up different commands and codes which a programmer uses to direct the computer's actions. In the early days of programming anyone who wanted to program had to learn Machine Language, and there is a different dialect for each brand of CPU. Today we teach and learn High Level languages, so called because they more closely resemble human language than machine language. Needless to say they are much easier to learn than Machine Language, but since they allow us a wide range of control over the machine, they are necessarily complex and hence rather daunting to a newcomer. I will try to assist you by simplifying the complexity,where possible, and helping you to understand it where the complexity is necessary. Some programming is complex and there is no way to totally avoid it.

Now let's develop this metaphor of learning a new language further. In programming the basic element of instruction or communication is the command or statement. It is comparable to a word or a phrase in human languages. Sometimes a command consists of one word; sometimes it consists of a short combination of words: a phrase if you will. A command tells the computer to do one thing. For simple actions a command is sufficient, but just as in human communication we sometimes want to communicate something more complex. This requires that we use several commands or statements in a sequence. In human language we combine words and phrases into sentences. In programming the comparable term to a sentence is a line. As a sentence should be a complete thought, so a line of code should be a complete action. For practical purposes you can think of a statement as consisting of one line of code. In Visual BASIC we can actually put several commands on the same line. Textbooks will tell you that these commands need to be separated by a colon in order to be successfully placed on the same line. This is one of the rules of the Visual BASIC programming languages. In human language we call this grammar or syntax. In computer languages we call it syntax.

One of the most important things to learn as a beginning programmer is to make your code readable and accessible. One of the most effective ways of destroying readability and accessibility is to put more than one command on a line. The only acceptable reason for doing so is to speed up the execution of the program, and this only happens for interpreted languages. So as a beginning programmer you should not put more than one command on a line. Sometimes the editor will insert a colon for you and place two statements on one line. If you see it do so you should eliminate the colon and insert a carriage return (enter key) in its place.

We write a paper, to make a point. To do this we often create sections in the paper, composed of several paragraphs. The paragraphs combine to establish our sub points. The sub points combine to establish the major point. In human language we express a complex thought in several sentences, called a paragraph. This is the same in programming. We group several lines of code together to get the computer to perform a more complex action (paragraph). We put spaces into our code to separate statements as we separate paragraphs. In programming the analogous structure to a section is a procedure. Each procedure would contain one major subtask. Several subtasks combine to create our program. The statements in each procedure must be ordered correctly just as we would order our arguments in a written article. The key in programming, as in good writing, is to limit each procedure, to only doing just one task. Where the task to be accomplished is complex, we use more procedures and break up our task into several pieces. In writing each supporting facts or idea is developed in its own paragraph and several paragraphs are required to establish our main idea or thesis. In programming each main idea is organized as a module. The module is composed of procedures, functions or methods. You will learn more of these other structures later. The collection of all these procedures, functions and methods makes up our program. A program is a self contained collection of code that performs a specific set of tasks.

Visual BASIC used to be described is an event-driven language. An event is triggered when a user interacts with a control on the program's form. The event is linked to a section of code, called a procedure, and the event causes the procedure to execute thus responding to the user's action. When events are linked to procedures in this way we refer to the procedure as an Event Procedure. In this way the user's actions are linked to the code which the programmer writes and will respond when the control is triggered. So as programmers we are required us to think algorithmically. It helps to work backwards here. Start with the end result and then plan our way back to where we are when the program starts. The process goes something like this:

  1. What set of tasks do we want our program to do, (What output is being created, or where do we want to end up?)
  2. What input will this set of tasks require? (Where are we now?)
  3. What events do we need to create? (What actions will the user take to cause the program to produce the output?)
  4. What procedures do I need to write to respond to the users actions? (What actions do we need to take in each procedure we write for step three above?)

In actual fact we are controlling the user by controlling the premises of the decisions that the user makes. This is much more subtle that dictating to the user what they should do, and much more effective too! VB is still an event-driven language, but In its latest incarnation it has become more. It is now also a fully Object Oriented language. Object Oriented languages were introduced in the 1970's and became fairly common place by the 1980's. It will take a little longer to explain and understand what this means. For now think of it as a way of organizing our programs which increases program reliability, and reduces the time it takes to write the program. Neither of these advantages is considered trivial! Any modern programming language has a significant learning curve in order to produce non-trivial useful programs. Visual BASIC has one of the smaller learning curves. You can learn it faster than most of the alternatives and yet it is about as powerful as most other languages. This makes it an excellent choice of language for a beginner to learn how to program.