Home

Introduction to the Visual Basic Language

 

The Visual Basic Language in Microsoft Visual Basic

 

Introduction

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.

Console Applications

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.

The Compiler

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.

Console Program Creation

 

Introduction

To create a console application in Microsoft Visual Basic, after starting your programming environment:

  • If you are using Microsoft Visual Basic 2008 Express, on the main menu, you can click File -> New Project...
  • If you are using Microsoft Visual Studio 2008 Professional, on the main menu, you can click File -> New -> Project... In the Project Types list, click Visual Basic.

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:

New Project

When you are ready to create the project, click OK.

A Program's File

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.

Introduction to Modules

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.

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 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 can 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().

The MsgBox() procedure provides more details. We will come back to it in Lesson 7.

 

Practical Learning: Introducing the Visual Basic Language

  1. Start Microsoft Visual Basic (2008 Professional or 2008 Express Edition)
  2. To create a new application, on the main menu, click File -> New -> Project... (or File -> New Project...)
  3. In the Project Types list, make sure Visual Basic is selected.
    In the Templates list, click Console Application
  4. Set the Name to Exercise
     
    New Project
  5. Click OK
  6. To use the MsgBox procedure, change the file as follows:
     
    Module Module1
    
        Sub Main()
            MsgBox("This is the wonderful world of VBasic")
        End Sub
    
    End Module
  7. To execute the application, on the main menu, click Debug -> Start Without Debugging
     
    Message Box
  8. After seeing the result, click OK on the message box
     
    Console
  9. Press Enter to close the DOS window
 

A Solution and its Projects

 

A Solution

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.

Practical Learning: Creating a Solution

  1. To start a new project with its solution, on the main menu, click File -> New -> Project (or File -> New Project)
  2. In the Templates list, click Console Application
  3. In the Name Text box, type StudentsRecords
  4. In the Solution Name, type SchoolManagement
     
  5. Click OK
  6. Notice the name of the solution on the title bar and the name of the project in the Solution Explorer
     
    Solution
  7. Click under the Sub Main() line and type msgbox("First Project")

Working on Additional Projects

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.

Practical Learning: Adding a Solution

  1. To add a new project to the current solution, on the main menu, click File -> Add -> New Project
  2. In the Add New Project dialog box, click Console Application
  3. In the Name Text box, type FinancesAndAccounting
     
    Add New Project
  4. Click OK

A Project and its Files

 

Adding a File to a Project

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:

  • On the main menu, you can click Project -> Add New Item...
  • In the Solution Explorer, you can right-click the name of the project, position the mouse on Add, and click New 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.

Opening a File

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:

  • Double-click the file
  • Right-click the file and click Open
  • On the main menu, click File -> Open -> File... Locate the folder of the project. In most cases, the file would be located in the Debug folder inside the bin folder of the project. After locating and selecting the file, click it and click Open

Renaming a File

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.

Practical Learning: Renaming a File

  1. In the Solution Explorer, under StudentsRecords, right-click Module1.vb and click Rename
  2. Type Registrations and press Enter
  3. Still in the Solution Explorer, under FinancesAndAccount, right-click Module1.vb and click Rename
  4. Type Payroll and press Enter
  5. In the Solution Explorer, click StudentsRecords to select it
  6. On the main menu, click Debug -> Start Without Debugging
  7. In the Solution Explorer, under FinancesAndServices, double-click Payroll and

Adding an External File

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;

  • On the main menu, you can click Project -> Add Existing Item...
  • In the Solution Explorer, you can right-click the name of the project to which you want to import the file, position the mouse on Add, and click Existing Item...

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.

Removing a File From a Project

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.

Accessories for Code Writing

 

Comments

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

Alternatively, you can start a comment with the Rem keyword. Anything on the right side of rem, Rem, or REM would not be processed. Here is an example:

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.

White Spaces

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

Sub Main()

and

Sub                                      Main()

would produce the same effect.

 
 

Home Copyright 2008 FunctionX Next