Vb Net Console Tutorial



Vb Net Console Tutorial

In the previous topic, we have installed Visual Studio 2019 and created a console-based project with the name MYConsoleApp1. Now in this project, we will create our Hello world VB.NET program.

A VB.NET define the following structure to create a program:

Vb net console tutorial java
  • Namespace declaration
  • Procedure can be multiple
  • Define a class or module
  • Variables
  • The Main procedure
  • Statement and Expression
  • Comments

Unfortunately, Visual Basic doesn't support console applications. Even if you use the Win32 API for writing into the console screen, Your application won't work, because the Visual Basic compiler always creates GUI application and it doesn't provide any compiler options for changing it to console application. Learning Visual Basic.NET? Check out these best online Visual Basic.NET courses and tutorials recommended by the programming community. Pick the tutorial as per your learning style: video tutorials or a book. Free course or paid. Tutorials for beginners or advanced learners. Check Visual Basic.NET community's reviews & comments.

Create a Hello_Program.vb file in MYConsoleApp1 project and write the following code:

Hello_Program.vb

Let's compile and run the above program by pressing the F5 key, we get the follwoiing output.

Output:

An Alternate method to compile and execute the VB.NET program

We can also compile and execute the VB.NET program using the Command prompt instead of using the Visual Studio IDE.

Step 1: After creating and saving the Hello_Program.vb file in the MYConsoleApp1 project, open the command prompt and execute the commands, as we have shown in the prompt.

Vb console app

Vb Console Print

In place of MYConsoleApp1you can write your project name.

Step 2: After that, write vbc Hello_Program.vb, as shown below.

Step 3: If there is no error found at compile-time, it transfers the control in the next line for generating the Hello_Pogram.exe file.

Step 4: Type Hello_Program to run the program. We get the following output.

Output:

Now we will understand the basic structure of the VB.NET program:

  • In VB.NET programming, the first line of the program is 'Import System', where Imports is a statement that inherit the system namespace. A System is a namespace that contains basic classes, reference data types, events, attributes, and various inbuilt functions that help to run the program.
  • The Second line defines the Module It specifies the VB.NET filename. As we know, VB.NET language is a completely object-oriented language, so every program must contain a module or class in which you can write your program that contains data and procedures within the module.
  • You can define more than one procedure in classes and modules. Generally, the procedure contains executable code to run. A procedure may contain the following function:
    • Function
    • Operator
    • Sub
    • Get
    • Set
    • AddHandler
    • RemoveHandler
  • Every program must contain a Main() method. In VB.NET, there is a Main() method or procedure that represents the starting point to execute a program, as we have seen in C language, their entry point is the main() function.
  • A comment (') symbol is used to comment on a line that is ignored by the compiler in a program, and it is a good practice to use comments for a better understanding of the program.
  • The Console.WriteLine() is a method of the console class. It is used to print any text or messages in the application. And the Console.ReadKey() is used to read a single character from the keyboard back to the Visual Studio IDE.

Create a VB.NET program using Windows Form

If you want to create a new Window-based project in Visual Studio, follow the steps given below:

Step 1. Start the Visual Studio IDE.

Step 2. To create a project, click on File -> choose-> New-> Project

Vb.net Tutorial

The following window appears on the screen.

Step 3: Select Windows Forms App (.NET Framework) and click on the Next button.

Step 4: Provide the Project name and location to store the project file using the browse button in Location.

Step 5: Click on the Create button. The following window appears on the screen.

Application

Vb.net Console Tutorial Pdf

Step 6: Now double click on the middle area of Form1.vb (Design) file, it shows the following code.

Form1.vb

Vb Net Console Tutorial Java

Step7: Save file as Form1.vb.

Vb Net Console Tutorial Android Studio

Step 8: To compile and run the Form1.vb file, press F5 button or Start button in Visual Studio. It shows the following output.

Vb Console App