Aristides S. Bouras

Visual Studio Community – Writing and Executing a Visual Basic Program

The first thing you must do is create a new Visual Basic project. Visual Studio Community provides a wizard to help you do that. Start Visual Studio Community and from the window that opens (Figure 1), click on the ā€œCreate a new projectā€ button.

Figure 1 Starting a new project from Visual Studio’s main menu

Alternatively, if you click on the ā€œContinue without codeā€ link, then, from Visual Studio’s main menu you can select ā€œFile → New → Projectā€ as shown in Figure 2.

Figure 2 Starting a new project from Visual Studio’s main menu

The ā€œCreate a new projectā€ dialog box appears. In the right pane, select ā€œConsole Appā€ (prefer the one that can run on .NET Core on Windows, Linux and MacOS) as shown in Figure 3, and click on the ā€œNextā€ button.

Figure 3 Selecting the Visual Basic Console App

In the ā€œConfigure your new projectā€ window that opens (see Figure 4), in the ā€œProject nameā€ field, you need to enter the name of your project. Type ā€œtestingProjectā€ and click on the ā€œCreateā€ button.

Figure 4 Configuring your new project

In the next dialog box that appears, select the ā€œ.NET 8.0 (Long-term support)ā€ framework.

The project is created and opened in your Visual Studio environment. You should see the following components (Figure 5):

  • the ā€œSolution Explorerā€ window, which contains a tree view of the components of the projects, including source files, libraries that your code may depend on, and so on
  • the ā€œSource Editorā€ window with the file called ā€œProgram.vbā€ open. In this file you can write your Visual Basic code. Of course, one single project can contain many such files
  • the ā€œOutputā€ window in which Visual Studio displays messages useful for the programmer
  • other windows, such as the ā€œPropertiesā€ window.
Figure 5 Viewing the ā€œSolution Explorerā€, ā€œSource Editorā€, and ā€œOutputā€ windows in Visual Studio

You have just seen how to create a new Visual Basic console project. Let’s now write the following (terrifying, and quite horrifying!) Visual Basic program and try to execute it.

Imports System

Module Program
  Sub Main(args As String())
    Console.WriteLine("Hello World!")
    Console.WriteLine("Hallo Welt!")
    Console.WriteLine("Bonjour le Monde!")
  End Sub
End Module

The statement that displays the English message ā€œHello World!ā€ has been already entered for you automatically. Place your text cursor at the end of that line and hit the ā€œEnter ā¤¶ā€ key. Let’s try to type the second statement, the one that displays the German message ā€œHallo Welt!ā€. Type only the first character, ā€œCā€, from the Console statement by hitting the ā€œCā€ key on your keyboard. A popup window appears, as shown in Figure 6. This window contains all available Visual Basic statements, and other items that begin with the character ā€œCā€.

Figure 6 The popup screen in the Source Editor window

You can highlight a selection by using the up and down arrow keys on your keyboard.

Type the second character, ā€œoā€, from the Console statement. Now the options have become fewer. Select the option “Console” using the down arrow key from your keyboard  (if necessary), as shown in Figure 7.

Figure 7 The popup screen in the Source Editor window

Hit the period ā€œĀ .Ā ā€ key. The statement is automatically entered into your program. Select the “WriteLine” option as shown in Figure 8 and hit the ā€œopening parenthesis (ā€ key.

Figure 8 The popup screen in the Source Editor window

Complete the statement by writing Console.WriteLine("Hallo Welt!").Then, continue typing the rest of the Visual Basic program (as shown in Figure 9). Save the changes that you have just made. Your Visual Studio Code environment should look like this.

Figure 9 Entering a Visual Basic program in the ā€œProgram.vbā€ file

And now, let’s try to execute it! From the main menu, select “Debug → Start Without Debugging”.

The Visual Basic program executes, and the output is displayed in the “Debug Console” window, as shown in Figure 10.

Figure 10 Viewing the results of the executed program in the ā€œDebug Consoleā€ window

Notice: Alternatively, you can execute a file by hitting the CTRL + F5 key combination.

Congratulations! You have just written and executed your first Visual Basic program!

Notice: You should always save your changes before executing a program. To do so, from Visual Studio’s main menu you can select ā€œFile → Saveā€ or hit the ā€œCTRL + Sā€ key combination!

Now let’s write another Visual Basic program, one that prompts the user to enter their name. Type the following Visual Basic program into Visual Studio and hit CTRL + F5 to execute the file.

Imports System

Module Program
  Sub Main(args As String())
    Dim name As String

    Console.Write("Enter your name: ")
    name = Console.ReadLine()
    Console.WriteLine("Hello " & name)
  End Sub
End Module

Once you execute the program, the message ā€œEnter your name:ā€ is displayed in the ā€œDebug Consoleā€ window. The program waits for you to enter your name, as shown in Figure 11.

Figure 11 Viewing a prompt in the ā€œDebug Consoleā€ window

Type your name and hit the ā€œEnter ā¤¶ā€ key. Once you do that, your computer continues executing the rest of the statements. When execution finishes, the final output is as shown in Figure 12.

Figure 12 Responding to the prompt in the ā€œDebug Consoleā€ window

Press any key to close the window.

error: Content is protected !!