Fundamentals of Visual Basic

Introduction

Microsoft Visual Basic is a programming language that can be used to create various types of computer applications, also called apps, also called computer programs, or just programs. The popularity of Visual Basic came from the "visual" aspect in its name. This consists of creating beautiful graphical applications by selecting objects, named Windows controls, and adding them to another object, a form. This approach makes application developement intuitive and fast. Of course, code is also necessary to control the behavior of the intended finished product. Behind-the-scenes, Microsoft Visual Basic is a full-blown computer language with many attractive features that you should be aware of before deciding to create graphical applications from it.

In our lessons, we are going to study the language that serves as a foundation to Visual Basic.

Introduction to Projects

The primary job of an application is to show its results on the computer monitor, and there are various types of applications. A console application is a program that displays its results in a black window referred to as a DOS window. In our lessons, we will create those types of applications.

To create an application, you start with a project. In your project, you will have computer files in which you write some instructions. This is called a source file. A source file that contains Visual Basic instructions is a regular, simple, text-based file. It has the extension .vb. A good way to create an application is to use a programming environment. In this case, you can use Microsoft Visual Studio or Visual Studio Code. Both are freely available from the Microsoft website. Simply do a search on "visual studio". Find the webpage and link to Microsoft Visual Studio Community Edition, download, and install it to your computer. For most of our lessons, we will use Microsoft Visual Studio to study the Visual Basic language.

The Starting Code of Visual Basic

As mentioned already, you use text-based files to write the instructions that would produce the results you want from your application. These instructions are also called "code". The text-based file you use presents a document we refer to as a Visual Basic document. In our lessons, we will learn what instructions or code you can write and why. Still, there is some fundamental code that your Visual Basic document should have. That primary content is as follows:

Module Program
    Sub Main()

    End Sub
End Module

Throughout our lessons, we will learn what everyone of those words and symbols mean. For now, simply use that code as is.

Practical LearningPractical Learning: Starting a Visual Basic Project

  1. Start Microsoft Visual Studio
  2. In the Visual Studio 2026 dialog box, click Create a New Project

    Create a New Project

  3. In the Create a New Project dialog box, click All Languages
  4. In the menu that comes up, click Visual Basic
  5. In the large list on the right side of the dialog box, and click Console App (it should be selected already)

    Create a New Project

  6. Click Next
  7. Change the Name to Exercise1
    Accept or change the Location

    Configure Your New Project

  8. Click Next
  9. In the Additional Information wizard page, click the arrow of the Framework combo box and select the latest version (.NET 10.0 (Long Term Support))

    Additional Information

  10. Click Create

Building a Program

Code you write for your project is in plain English. The computer doesn't understant that language. The instructions you write must be translated in a language the computer can understand. That language is called machine language.

To translate the instructions from English to machine language, you use a program named a compiler. In reality, a compiler is a group of sub-programs that accomplish various goals to get a functional program at the end. The end result is a program that can be executed in a computer. That final program is called an executable.

As mentioned already, a compiler is a computer program made of internal other sub-programs. One of the sub-programs, in fact probably the first, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks the syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the programs or troubleshoot the problems.

If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its result(s) to the compiler. The compiler calls another program called a linker. If the program contains just one file, the linker considers it. If the program contains more than one file, the linker considers them.

The linker gathers some of the files that the compiler shipped with (those files that your program needs in order to work), puts them together ("links" them) with your file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant problem, the compiler creates the executable. This doesn't mean that everything is alright, it only means that the compiler thinks that everything is alright: it is still possible that the result may not be what you would expect. We will come back to these issues.

To make your life easier, all of the sub-programs (parser, linker, debugger, etc) that ship with Visual Basic are grouped in one large program: the compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to translate your English instructions into a machine language.

To make the compiler check that your program is valid and ready, you can build it. Microsoft Visual Studio provides a menu item to do that, under the Build menu category.

Executing a Program

After building your code, you and your users can execute it. If you are working in either Microsoft Visual Studio or Visual Studio Code, they provide easy menu options to assist you.

A Visual Basic Solution and its Projects

Creating a Project

Although you can create a complete and fully functional application using a text editor, Microsoft Visual Studio provides a graphical environment that is more convenient. You start by creating a project.

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

The Solution Explorer

The Solution Explorer is a window that displays a list of the files that make up a project. To access the Solution Explorer:

Managing a Solution

Opening a Project

There are a various ways you can open an existing project:

Adding a File to a Project

As mentioned already, a project can contain many files. If you are using Microsoft Visual Studio, to add a new file to a project:

This would display the Add New Item dialog box. From there, select the type of file you want. Accept or change its name. Then click Add.

When a project is made of various files, each file is represented by a label in the top section of the Code Editor.

Accessing a File

If you are using Microsoft Visual Studio, if a file is a member of the current project, to open it:

Opening a File

To open a file:

This would display the Open File dialog box. From there, locate the file from its folder and click it.

Writing Code

After creating or opening a file, you can add the necessary code to it. Microsoft Visual Studio ships with many skeleton codes you can use and customize. It writes the primary code for you and may add some default behaviors. Once it has done this, you can change or remove any section. To access these code skeletons, in the section of the document where you want to add it, right-click and click Insert Snippet... In the menu that comes up, double-click a category. Then double-click another category. This would display a list of code types. If you see the type of code you want to use, double-click it.

The Starting File of a Project

If you add many files to a project, you must specify what file should be processed first. If you create a console application using Microsoft Visual Studio, the first file you create is set as the default. To specify the starting file of your project, on the main menu, click Project -> project-name Properties... In the Startup Object combo box, select the file.

Code Colors

In Microsoft Visual Studio, code is written in a wide area with a black or white background. This is the area you use the keyboard to insert code with common readable characters. The Code Editor uses some colors to differentiate categories of words or lines of text.

The colors used are customizable. To change the colors, on the main menu, click Tools -> Options... In the Options dialog box, in the Environment section, click Fonts and Colors. To set the color of a category, in the Display Items section, click the category. In the Item Foreground combo box, select the desired color. If you want the words of the category to have a colored background, click the arrow of the Item Background combo box and select one.

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 Exercise

    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 Exercise

    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

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() can be written as follows:

Sub Main()

or as follows:

Sub                                      Main()

Home Copyright © 2008-2026, FunctionX Monday 15 December 2025, 08:59 Next