Before we look at the fundamental building blocks of the VB.Net programming language, let's look at a simple VB.Net program structure that we can use as a reference in the following next topics.
A VB.Net program is made up of the following components:
- Declaration of a namespace
- A type of class or module
- a technique or procedures
- Variables
- The primary procedure
- Expressions and statements
- Comments
Let's have a look at a simple code that prints the words "Hello World."
Imports System
Module Module1
'This program will display Hello World
Sub Main()
Console.WriteLine("Hello World")
Console.ReadKey()
End Sub
End Module
The output of the above program after compilation and execution will be
Hello, World!
Let us understand the different components of the above code.
- The program's first line, Imports System, includes the System namespace in the application.
- The following line provides a Module declaration, Module1. Because VB.Net is entirely object-oriented, every program must include a class module that holds the data and operations that your program employs.
- In most cases, Classes or Modules will contain more than one procedure. Procedures contain executable code, or in other words, they define the class's behaviour. Any of the following procedures could be used as a procedure.
- Function
- Sub
- Operator
- Get
- Set
- AddHandler
- RemoveHandler
- RaiseEvent
- The following line (This program) will be disregarded by the compiler and added to the program to add more comments.
- The following line specifies thebehaviourocedure, which serves as the starting point for all VB.Net projects. When the module or class is executed, the Main procedure determines what it will perform.
- The statement determines the behaviourand available of the Main procedure.
Console. LineWriter ("Hello World") WriteLine is a method of the System Namespace’s Console class. The message "Hello, World!" is displayed on the screen due to this statement.
- The final line is Console.
ReadKey() is only available to VS.NET users. When the program is launched from Visual Studio.NET, this will prevent the screen from running and closing rapidly.
Compile & Execute VB.Net Program
Take the following steps if you are using the Visual Studio.Net IDE:
- Launch Visual Studio.
- Select File New Project from the navigation bar.
- Select Visual Basic from the available templates.
- Select Console Application.
- Using the Browse button, enter a name and location for your project, and then click the OK button.
- The new project is shown in Solution Explorer.
- In the Code Editor, enter code.
- To run the project, press the F5 key or the Run button. A Command Prompt window with the line Hello World appears.
You may compile a VB.Net software without utilizing the Visual Studio IDE by using the command line.
- Open a text editor and paste the code mentioned above into it.
- Make a copy of the file and save it as “helloWorld.vb”.
- Navigate to the directory where you saved the file using the command prompt tool.
- To build your code, type “vbc helloworld.vb” and hit enter.
- If your code contains no mistakes, the command prompt will forward to the next line and create the helloworld.exe executable file.
- Then, to run your program, type helloworld.
- The words "Hello World" will appear on the screen.