Introduction to the Visual Basic Language
The Visual Basic Language in Microsoft Visual Basic
Microsoft Visual Basic is a production environment used to create computer applications for the Microsoft Windows family of operating systems. The environment is fairly easy to use but you must know its "language". At its foundation, Microsoft Visual Basic uses a language that has been developed and improved over the years. It started with Basic but has had incremental flavors such as QuickBasic or QBasic.
Microsoft Visual Basic is used to create graphical applications, also referred to as Graphical User Interface (GUI) applications, web-based applications, and other types of applications. In order to effectively create these applications, you must be familiar with the language used in this programming environment. Most other programming environments use a known but separate language. For example, Microsoft Visual C++ 2005 uses many languages including C, C++, and C++/CLI. Borland Delphi uses the Object Pascal language, Microsoft Visual C# uses the C# language, Borland JBuilder uses the Java language. There is no official name for the language used by Microsoft Visual Basic. In our lessons, we will call the "Visual Basic Language".
In our lessons, we are going to study the language that serves as a foundation to the Microsoft Visual Basic programming environment.
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. To follow these lessons, you must have an environment that allows you to create these types of applications using the Visual Basic language.
If you have already installed Microsoft Visual Studio 2008 Professional, you should be ready to follow these lessons. As an alternative, Microsoft created and made freely available Microsoft Visual Basic 2008 Express Edition. To get Microsoft Visual Basic 2008 Express Edition, you can access the MSDN section of the Microsoft web site and look for a link that displays Visual Studio Express Editions, then download Microsoft Visual Basic 2008 Express Edition. After downloading it, you must install and register it.
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 machine. The instructions are written in plain, easily understandable English, 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.
The Microsoft Visual Basic language includes a compiler named vbc. Like most other programs, it has the extension .exe. Therefore, it is called vbc.exe.
To create a console application in Microsoft Visual Basic, after starting your programming environment:
In both cases, in the Templates list, you will click Console Application. In the Name box, accept the suggested name or give a name of your choice:
When you are ready to create the project, click OK.
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 Visual Basic 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 our lessons. The code that is conform to the Visual Basic 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 Visual Basic 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.
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 Visual Basic 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
We will come back to procedures in another lesson. For now, the most fundamental procedure used in Visual Basic is called Main. In a program, the Main procedure is the entry point. That is where the program starts. When you create a console application using the New Project dialog box, sample code 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 Visual Basic 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:
To distinguish a procedure from other items used in a program, we will sometimes write it followed by parentheses. Examples are Main() and MsgBox().
Practical Learning: Introducing the Visual Basic Language
Module Module1 Sub Main() MsgBox("This is the wonderful world of VBasic") End Sub End Module
Throughout our lessons, we will learn how to create (console) applications. These are created from a project. So far, we have been able to create a project. In reality, such a project is created inside of another ensemble referred to as a solution. A solution is automatically created for you when you start a new project. We will learn that you can add as many projects as you want, to a solution.
When you start a new application from the New Project dialog box, you are asked whether you want to create a new solution too, and you must give it a name. By default, the new project and solution would hold the same name. If you accept the suggestions, you would get a main folder with the name of the project. Inside of that folder, there would be a folder with the same name. That inside folder would represent the project.
After creating a solution, its name appears on the title bar of Microsoft Visual Studio. The name of the project would appear in the Solution Explorer.
So far, we were working on a solution with a single project. In some cases, you may want to work on various projects. Instead of always creating a new project, you can add a new project to a solution you are working on.
To add a new project to a solution, on the main menu, click File -> Add -> New Project... This would display the Add New Project dialog box. In the Templates list, select the type of project you want. Accept or change the name of the project, then click OK.
The names of the different projects are listed in the Solution Explorer.
In our projects so far, when we created a project, a default file was created for it. If necessary, you can add as many files as necessary to a project. To add a file to a project:
This would display the Add New Item dialog box with the name of the project on the title bar. In the Templates list, you can select the type of file you want to create, give it a name, and click Add or press Enter.
Once a file has been added to a project, you can use it. Some operations require that the file be opened. To open a file, in the Solution Explorer, under the name of the project:
In the introduction we have had so far, we learned to create a small console projects with one file. That first file was named Module1. Because this name can be too generic or vague, you can change it at any time.
To rename a file, in the Solution Explorer, under the name of the project that holds the file, right-click it and click Rename. Then type a name with the .vb extension and press Enter.
When working on a project, if there is a file in another project and you want to use that file in your project, you can import that file. To do this;
Any of these actions would display the Add Existing Item dialog box with the name of the project on the title bar. Locate the file, select it, and click Open.
If you have a file in a project but do not need that file anymore, you can delete it. To remove a file, in the Solution Explorer, under the project, right-click the name of the file and click Delete.
A comment is text that the compiler does not process when reading your code. As such, a comment can be written any way you want. In the Visual Basic language, the line that contains a comment can start with a single quote. Here is an example:
Module Module1 Sub Main() ' This line will not be considered as part of the code End Sub End Module
Module Module1 Sub Main() ' This line will not be considered as part of the code Rem I can write anything I want on this line End Sub End Module
Comments are very useful and you are strongly suggested to use comments regularly. They can never hurt your code and they do not increase the size of your application. Comments can help you and other people who read your code to figure out what a particular section of code is used for, which can be helpful when you re-visit your code after months or years of not seeing it.
When writing code, you will have to separate different parts with spaces, like the one between Sub and Main. This is referred to as white space. The amount of space you put between two words is not important. The compiler would ignore the white spaces between words. This means that Sub Main() and Sub Main() would the same effect. In the same way
would produce the same effect.
|Home||Copyright © 2008 Yevol||Next|