Structure of a Visual Basic Program
A VB.net Program Structure is constructed using standard components. One or more projects make up a solution. One or more assemblies can be found within a project. Each assembly is made up of several source files. A source file contains all of your code and contains the definition and implementation of classes, structures, modules, and interfaces.
Let’s look at a bare minimum VB.net program structure before we look at the basic building elements of the VB.Net programming language, so we can use it as a reference in the next chapters.
Visual Basic Program Structure : Table of contents
In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.
VB.net Hello World Example
A VB.net program is made up of the following components:
- Namespace declaration
- A class or module
- One or more procedures
- The Main procedure
- Statements & Expressions
Consider the following code, which prints the words “Hello World.”
When the preceding code is compiled and run, the following is the result:
Let us look various parts of the above program.
- The first line has a Module declaration, the module is completely object oriented, so every program must contain a module of a class that contains the data and procedures that your program uses.
- Classes or Modules generally would contain more than one procedure. Procedures contain the executable code, or in other words, they define the behavior of the class. A procedure could be any of the following:
- The next line defines the Main procedure, which is the entry point for all VB.Net programs. The Main procedure states what the module or class will do when executed.
- The Main procedure specifies its behavior with the statement Console.WriteLine(“Hello, World!”) WriteLine is a method of the Console class defined in the System namespace. This statement causes the message “Hello, World!” to be displayed on the screen.
How to Compile & Execute VB.net Program?
Before we start compiling and execute vb.net program, makes sure that you have already installed Microsoft Visual Studio in your computer
Time needed: 5 minutes.
Compile & Execute VB.net Program
- Step 1: Open Microsoft Visual Studio
First, open the microsoft visual studio IDE.
- Step 2: Create new project
Next, On the menu bar, choose File → New → Project.
- Step 3: Choose console app
Next, select console app and click next.
- Step 4 : Name the console project
Next, Specify a name and location for your project using the Browse button, and then choose the OK button.
- Step 5: The new project appears on the solution explorer
Next, as you can see the console project appears on the solution explorer.
- Step 6 : Write code in the Code Editor
Next, as you can see there’s already code in code editor area.
- Step 7 : Click run
Next, click the run button to execute the program.
- Step 8 : Output
Last, the console will display the output of the executed program.
Execute VB.net Program using Command Line
You can compile a VB.net program by using the command line instead of the Visual Studio IDE
1. Open Text Editor
Open a text editor and add the above mentioned code.
2. Save the File
Save the file as helloworld.vb.
3. Open CMD
Open the command prompt tool and go to the directory where you saved the file.
4. Compile your Code
Type vbc helloworld.vb and press enter to compile your code.
5. Generate Executable File
If there are no errors in your code the command prompt will take you to the next line and would generate helloworld.exe executable file.
6. Execute Program
Next, type helloworld to execute your program.
7. Display Output
You will be able to see “Hello,World!” printed on the screen.
A VB.net program is composed of various parts. After importing a namespace into a program, it becomes possible for us to use all the methods and functions that have been defined in that module. Every VB.net program must have a module.
The VB.net compiler ignores comments. We can have more than one procedures in a VB.net program.