VB.
NET Hello World Program
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:
Namespace declaration
Procedure can be multiple
Define a class or module
Variables
The Main procedure
Statement and Expression
Comments
Create a Hello_Program.vb file in MYConsoleApp1 project and write the following code:
Hello_Program.vb
Imports System 'System is a Namespace
Module Hello_Program
Sub Main()
Console.WriteLine("Hello, Welcome to the world of VB.NET")
Console.WriteLine("Press any key to continue...")
Console.ReadKey()
End Sub
End Module
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.
In place of MYConsoleApp1you can write your project name.
Step 2: After that, write vbc Hello_Program.vb, as shown below.
Advertisement
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:
Hello, Welcome to the world of VB.NET.
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.
Module Module1
End 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
Advertisement
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.
Step 6: Now double click on the middle area of Form1.vb (Design) file, it shows the following code.
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox("Welcome to the JavaTpoint")
End Sub
End Class
Step7: Save file as Form1.vb.
Step 8: To compile and run the Form1.vb file, press F5 button or Start button in Visual Studio. It shows the following output.