On this site, we are going to study the language that serves as a
foundation to the Microsoft Visual Basic programming environment. We will
call it VBasic. We don't want to call it Basic because it has grown far away
from its parent the simple Basic language that was at its origin. We don't want
to call it Visual
Basic because the word Visual supposes that we intend to create graphical
applications, which we will not do. The VBasic language on this site represents the language that Microsoft
uses to make the .NET Framework library, and the core language of Visual Basic, available to Visual Basic
programmers.
As opposed to a GUI application, a console application is
one that displays its results in a black window also called the DOS window. In
our lessons, we will create only console applications.
Command Line Fundamentals
|
|
When the computer starts, there is no primary visual or
graphical effect. The programs must first be loaded into memory. In fact, in the
beginning, the Microsoft operating system started as text-based with DOS 2.2,
3.3, 5.0, and 6.2 (6.2 was never a big hit like 5.0 because Windows 3.1, Windows
3.2, and Windows 3.3 were released at the same time, bringing the now attractive
"Windows" display). To create a program, a programmer entered the
source code using a text editor (there was a text editor called EDIT; it was
very cute, very basic, and very good; in fact, up to Windows 3.3, it was
still the text editor of choice of most programmers and it was used as the
editor for QBasic, the parent of Visual Basic). After creating the source file,
the programmer had to use a black (or "amber") screen that displayed a
blinking caret whose role was to let the user know that it was waiting for a
command.
A command was a word or a group of words that the programmer typed and
then pressed Enter (or Return). This caused the computer to perform an action. The
command was typed on a line. For this reason, the area that displayed the caret
was called the Command Line. Most low-level (low-level doesn't mean weak, to the
contrary) programs are still using this concept. This means that, behind the
scenes, computer languages such as VBasic, C/C++, Pascal, Java, C#, etc, are
still created and executed as if everything were done at the Command Line.
Command Line Understanding
|
|
To be an effective programmer, it is important to know how
the Command Line works. This can help you have a better understanding of things that are going on behind the scenes.
As its name indicates, the Command Line is a blank line with
a blinking caret that expects you to type a command:
There are various types of commands used on the command
prompt. Some of them are as old as the operating system. Such is the case for
commands used to create files or folders, copy files or folders, delete files or
folders, format a drive, etc. Some other commands are created by a person like
you who needs it for a particular application.
A computer program, also called a program, also called a
computer application, also called an application, is a series of instructions
that allows a person, called a user, to interact with the computer. This works by
the user giving instructions to the machine. Such instructions can be done by
performing actions such as clicking, typing, etc. To make this possible, a person
like you, also called a program developer, or a developer, or an application
developer, or a programmer, creates these instructions, decides what a
particular application can do, what it cannot do, and what it should not do.
Once the application has
been developed, it is then made available to users who must simply apply the
instructions that a programmer created.
The programmer is the person who creates instructions that a
user can apply on a computer application when interacting with the computer. The
instructions are written in plain, easily understandable language, using normal
computer files. Because the computer cannot understand them, the instructions
are handed to an intermediary program that can "translate" them in a
language the computer can understand. This intermediary application is called a
compiler.
To make programming easily accessible, Microsoft makes
available a free compiler that can be used to create normal, powerful,
applications using the VBasic language. If you use Microsoft Windows XP, Windows
Vista, or
Windows Server 2003, the compiler should already be installed in your computer.
If you use another Microsoft Windows operating system, you can get the compiler
from the Microsoft web site by downloading the .NET Framework.
The compiler used to create applications for VBasic is
called vbc. Like most other programs, it has the extension .exe.
To create VBasic applications using the vbc.exe application, you can first find it in your computer. By default, it is installed in a folder under
the Drive:\WINDOWS\Microsoft.NET\Framework. To use this free
compiler in all of your programs, you should include it in your system path.
Microsoft Visual Basic, and Express
|
|
To make it easy to learn VBasic and possibly create various
types of applications, Microsoft freely made available Microsoft Visual Basic
2005 Express Edition. You can download it free from the Microsoft web site.
In our lessons, we will use either Microsoft Visual Basic
(2005) Professional or Microsoft Visual Basic 2005 Express Edition.
To create a console program in Microsoft Visual Basic, after
starting it:
- If you are using Microsoft Visual Basic 2005 Express, on the main menu,
you can click File -> New Project...
- If you are using Microsoft Visual Basic 2005 or Microsoft Visual Studio
2005, on the main menu, you can click File -> New -> Project...
This would open the New Project dialog box. From there, you
can make your selection as we will indicate in future sections. You must also
give a name to your project. The New Project dialog box gives a default name to
the new project. If you want, you can keep that name. Otherwise, to change it,
replace the content of the Name text box.
As mentioned already, when creating your programs, you will
write text-based instructions in a file, also called a source file. A source file that contains VBasic instructions is a regular,
simple, ASCII, text-based file. It has the extension .vb as a Windows
file. To assist you with creating source files, Microsoft Visual Basic
provides a dialog box.
A program is started from a plain text file that has
the .vb extension. In this file, you write code as we will learn
throughout this site. The code that is conform to the VBasic language is
included in a section known as the module. This section starts with the
Module keyword followed by a name and ends with the End Module expression.
Everything between the Module keyword and the End Module
expression belong to
the same entity. Based on this, a simple file in VBasic would have the
following:
Module ModuleName
End Module
To create a source file, on the main menu, you can click
Project -> Add Module, or Project -> Add New Item. In the Add New Item
dialog box, make sure Module is selected or click it. In the Name text box,
accept the suggested name or change it. Then click Add.
Introduction to Procedures
|
|
A procedure is a section of code that takes care of a
specific task. In a module, the basic formula of a procedure is:
Sub ProcedureName()
End Sub
Notice that it ends with the End Sub line.
Because a module holds the code of a VBasic program, a procedure is
included inside the start and the end of the module section. This would be
done as follows:
Module ModuleName
Sub ProcedureName()
End Sub
End Module
The most fundamental procedure
used in VBasic is called Main. In a program,
the Main procedure is the entry point. That's where the program starts.
When you create a console application using the New Project dialog box, skeleton
would be written for you and it would include the Main procedure that would
appear as follows:
Module Module1
Sub Main()
End Sub
End Module
Another common procedure highly used
in VBasic is called MsgBox. As its name indicates, the MsgBox procedure is used to display a message
in a dialog box. To make this happen, you must include the message inside the
parentheses of MsgBox. The message itself should be included in
double-quotes. An example would be:
MsgBox("Whatever")
To distinguish a procedure from other items used
in a program, we will sometimes write it followed by parentheses. Examples
are Main() and MsgBox().
|