Deploy Containers Close to Your Users

This Engineering Education (EngEd) Program is supported by Section.

Instantly deploy containers globally. Section is affordable, simple and powerful.

Get Started for Free.

Getting Started with Visual Basic.NET

September 13, 2021

Visual Basic.net is a multi-paradigm, object-oriented programming language. We implement it in the .NET Framework developed by Microsoft. They invented Visual Basic in 2001 to replace Visual Basic 6.

Visual Basic.Net is pronounced as “Visual basic dot net“. We commonly abbreviate it as VB.NET.

Although VB.NET is a developed version of Visual Basic 6, it is not compatible with it, .i.e. we cannot compile code written in Visual Basic 6 under VB.NET.

Why VB.NET?

  • VB.NET is a beginner-friendly language.
  • With VB.NET, you can create web applications with modern features like performance counters, file systems, and event logs.
  • Since we implement VB.NET using the .NET framework, we can connect our applications to others created in languages that run on the same framework.
  • You will enjoy drag-and-drop capabilities to replace any elements that you may need.
  • The language is not case sensitive.

Although VB.NET may have a couple of advantages, it also has some drawbacks associated with it. They include :

  • VB.NET cannot handle pointers directly.
  • Since VB.NET is an intermediate language, many libraries are required for the Just In Time compiler to interpret the application.

To get started, you will need a development environment. This is where we will write and debug our code. We will use Visual Studio as our text editor.

Installing Visual Basic.Net 

After you have selected Visual Studio, choose .NET desktop to develop and click Modify.

vscode

After the installation, click Launch to get started. On the new window, choose Create a new project.

newproject

Once you have selected Create a new project, choose ” Install more tools and features" then click Next.

createproject

Pick Visual Basic from the drop-down menu, then select Console Application from the list and click Next.

picktemplate

Enter your preferred project name and click Next. Here, I will name my file hello world.

pickname

After selecting the target framework you desire, click Create. Visual Code will auto-generate some VB.NET code for you.

project

Let’s look at the code.

We have the code generated below. It’s a Hello World program.

Imports System

Module Program

    Sub Main(args As String())

        Console.WriteLine(“Hello World!”)

    End Sub

End Module 

Press the F5 key on your keyboard to run your program. This will compile and run your code. 

A console window will open with the following output:

Hello World!

C:\Users\sa\source\repos\helloworld\helloworld\bin\Debug\netcoreapp3.1\helloworld.exe (process 6544) exited with code 0.

To automatically close the Console when debugging stops, enable Tools->Options->Debugging->Automatically close the Console.

Press any key to close this window . . .

The Syntax 

We will look at each line of code from our program generated by Visual Studio. 

Line 1: 

Imports System

This line is used to include a namespace, system in our program. With that namespace, we will access all the methods defined in it without getting an error.

A Namespace can be defined as a group of codes that are related to each other. It majorly comprises pieces of code such as classes, procedures, and functions.

Line 2: 

Module Program

   code declaration

End Module 

A module defines a reference sort obtainable throughout its namespace. For our HelloWorld program, we have declared our module and named it Program. Inside this module, we will define methods to be used by our program. To close our module, we use the End keyword.

Line 3: 

Sub Main(args As String())

     code declaration 

End Sub

Our module has one procedure, Sub. A procedure is a block of code enclosed by a declaration statement with a matching end declaration. From this line execution of our program will begin. Each program in VB.NET should have a main method.

Line 4:


Console.WriteLine(Hello World!)

Console.WriteLine() prints out text on the console. After printing a string, it adds a new line at the end. In our program, it will output  “Hello World!”.

Alternatively, we can use Console.Write() instead of Console.WriteLine() .The major difference is that Console. Write() does not add a new line at the end of the string. We can find more about the two commands here.

Getting user input 

To read the next characters from a standard input stream, we use Console.ReadLine().

Console.Read() reads the next Character from the input.

Console.ReadKey() gets the next character pressed by the user. Then, we display the key in the console window.

Comments in Visual Basic .NET

Comments help a programmer understand what the code does at a particular stage. They are mainly used to make code readable and easy to understand.

The compiler usually ignores comments when compiling the code.

In VB.NET, comments start with a single apostrophe .

Example


    This is a comment in VB.NET.

Unfortunately, for Visual Basic, we do not have block comments. All comments are single-line comments.

Tips on writing good comments

  • Ensure that your comments are not a duplicate of the code you have written. Some programmers write comments that are very obvious and unnecessary.
  • When you find it hard to write a simple comment, your code might be where the problem is.
  • Your comments should clear any confusion, not create it.
  • Incorporate links to additional references where you feel they will be most helpful.
  • Write comments when fixing bugs, too.

Note: VB.NET follows top-to-bottom order when executing a program.

Congratulations! You just wrote your first VB.NET program.  

To learn a few more terms, we will write a simple program to add two numbers entered by the user.

Imports System

Module Program

    Sub Main(args As String())

        Dim firstNum, seconNum, sum As Integer

        Console.WriteLine(enter first number:)

        firstNum = Console.ReadLine()

        Console.WriteLine( enter second number:)

        seconNum = Console.ReadLine()

        sum = firstNum + seconNum

        Console.WriteLine(the sum is: & sum)

        Console.ReadLine()

    End Sub

End Module

Here’s how the output will look like when we input our first number as 2 and the second as 6 :

enter the first number:

2

 enter the second number:

6

the sum is:8

Let’s look at the new lines in this program.

Dim firstNum, seconNum, sum As Integer

This line declares variables that will store the user input and the result of the addition by the system.

As Integer means that the declared variables will be of the data type Integer Int. Apart from integers, we have other data types, such as 

  • String - stores any numerical, alphabetic, and special characters
  • Double - stores fractional numbers
  • Boolean - stores data as either True or False
  • Date - stores the date value and has a range of January 1, 0001 - December 31, 9999.      
firstNum = Console.ReadLine()

This line assigns the user input to our declared variable for storage. We can see that we repeat this step for the second input under a different variable. This is because a variable can only hold value for one input. So in case, we assign a second, it will replace the original one.

Conclusion

In this tutorial, we looked at :

  • Setting up a Visual Basic.NET development environment.
  • A sample of Visual Basic.NET that prints “Hello World”.
  • Writing a program to add two numbers from the user input.
  • Tips on writing good comments

Happy coding!


Peer Review Contributions by: Lalithnarayan C