Home

Introduction to VBasic

 

VBasic 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. 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++ uses the C++ language. 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. Because we want to distinguish that language from Microsoft Visual Basic, on this site, we will call it VBasic.

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:

Command Prompt

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.

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 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.

Console Program Creation

 

Introduction

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.

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 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.

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

 
 

Practical Learning: Introducing VBasic

  1. Start Microsoft Visual Basic (2005 Professional or 2005 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 Exercise1
     
  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
     
  8. After seeing the result, click OK on the message box and press Enter to close the DOS window
 

Accessories for Code Writing

 

Comments

A comment is text that the compiler doesn't consider when reading your code. As such a comment can be written any way you want. In VBasic, 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 read. 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 don't 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 © 2005-2012, FunctionX, Inc. Next