Introduction to VB.
Net
Visual Basic .NET (VB.NET) is an object-oriented computer programming language implemented
on the .NET Framework. It was first released in 2002 to replace Visual Basic 6. VB.NET is an
object-oriented programming language. This means that it supports the features of object-oriented
programming which include encapsulation, polymorphism, abstraction, and inheritance.
Visual Basic .ASP NET runs on the .NET framework, which means that it has full access to the
.NET libraries. It is a very productive tool for rapid creation of a wide range of Web, Windows,
Office, and Mobile applications that have been built on the .NET framework. Since VB.NET relies
on the .NET framework, programs written in the language run with much reliability and scalability.
With VB.NET, you can create applications that are fully object-oriented, similar to the ones created in
other languages like C++, Java, or C#. Programs written in VB.NET can also interoperate well with
programs written in Visual C++, Visual C#, and Visual J#. VB.NET treats everything as an object.
The VB.NET language is designed in such a way that any new beginner or novice and the ad-
vanced programmer can quickly develop a simple, secure, robust, high performance of web, windows,
console, and mobile application running on .NET Framework.
Features of VB.NET
• It is an object-oriented programming language that follows various oops concepts such as ab-
straction, encapsulation, inheritance, and many more. It means that everything in VB.NET
programming will be treated as an object.
• VB.Net is used to design user interfaces for window, mobile, and web-based applications.
• It supports a rapid application development tool kit. In which a developer does not need to write
all the codes as it can get various code automatically from its libraries. For example, when we
create a form in Visual basic.net, it automatically calls events of various form in that class.
• It is not a case sensitive language like other languages such as C++, java, etc.
• It supports Boolean condition for decision making in programming.
• It also supports the multithreading concept, in which you can do multiple tasks at the same
time.
• It provides simple events management in .NET application.
• A Window Form enables us to inherit all existing functionality of form that can be used to
create a new form. So, in this way, it reduced the code complexity.
• It uses an external object as a reference that can be used in a VB.NET application.
• Automatic initialized a garbage collection.
• It follows a structured and extensible programming language for error detection and recovery.
• Conditional compilation and easy to use generic classes.
• It is useful to develop web, window, and mobile applications.
1
Advantages of VB.NET
• The VB.NET executes a program in such a way that runs under CLR (Common Language
Runtime), creating a robust, stable, and secure application.
• It is a pure object-oriented programming language based on objects and classes. However,
these features are not available in the previous version of Visual Basic 6. That’s why Microsoft
launched VB.NET language.
• Using the Visual Studio IDE, we can develop a small program that works faster, with a large
desktop and web application.
• The .NET Framework is a software framework that has a large collection of libraries, which
helps in developing more robust applications.
• It uses drop and drag elements to create web forms in .NET applications.
• Visual Basic .NET allows to connect one application to other applications created in languages
that run on the .NET framework.
• A VB.NET can automatically structure your code.
• The Visual Basic .NET language is also used to transfer data between different layers of the
.NET architecture such that data is passed as simple text strings.
• It uses a new concept of error handling in the Visual Basic .NET Framework. The new struc-
ture is the try, catch, and finally method used to handle exceptions as a unit. In addition, it
allows appropriate action to be taken at the place where it encountered an error. In this way, it
discourages the use of the ON ERROR GOTO statement in .NET programming.
Disadvantages of VB.NET
• The VB.NET programming language is unable to handle pointers directly. Because in this
language, it requires a lot of programming, and it is not easy to manage every address by a
pointer. Furthermore, additional coding takes extra CPU cycles that increases the processing
time. It shows the slowness of the VB.NET application.
• The VB.NET programming is easy to learn, that increases a large competition between the
programmers to apply the same employment or project in VB.NET. Thus, it reduces a secure
job in the programming field as a VB.NET developer.
• It uses an Intermediate Language (IL) compilation that can be easily decompiled (reverse engi-
neered), but there is nothing that can prevent an application from disintegrating.
• Just-In-Time (JIT) compiler: It is the process through which a computer can interpret IL (inter-
mediate language) compilation and is also required to run your application. It means that the
target computer needs a JIT compiler to interpret a source program in IL, and this interpretation
requires an additional CPU cycle that degrades the performance of an application.
• It contains a large collection of libraries for the JIT compiler that helps to interpret an applica-
tion. These large libraries hold a vast space in our system that takes more computing time.
2
Difference between VB.Net and VB
VB.Net VB
It stands for Visual Basic. Network Enables Tech-
nology. It is also developed by Microsoft, and this It is a programming language developed by Mi-
language was based on the .Net Framework. Fur- crosoft for the fastest development of a window-
thermore, it is specially designed for VB develop- based operating system as well as applications.
ers.
VB is the predecessor of VB.NET and was not
It is a modern, fully object-oriented language that
an object-oriented language. So, it is not actively
replaced VB6.
maintained.
A VB.NET uses the Common Language Runtime
Visual Basic uses the VB-Runtime environment.
(CLR) component of .Net Framework at runtime.
It is a compiled language It is an Interpreter based language
It does not support backward compatibility. It supports backward compatibility.
It is a type-safe language. It is not a type-safe language.
Data Connectivity and handling are done through
In VB.NET, data is handled using the ADO.net
DAO, RDO, and ADO (ActiveX Data Object) pro-
protocol.
tocol.
The Object support default property of visual ba-
Object does not support default property.
sic.
In the VB.Net parameter are passed by a default In VB, most of the parameters are passed by refer-
value. ence
A Multithreaded application can be developed in
It does not support the multithread concept.
VB.NET.
Modules in VB.Net
A VB.Net program consists of the following modules:
• Namespace declaration
• One or more procedure
• A class or module
• Variables
• The Main Procedure
• Comment
• Statement and Expression
Given below is a simple hello world program in VB.Net
Imports System
Module Program
Sub Main()
’This is a simple VB.Net Program to print hello world in console
Console.WriteLine("Hello World!")
Console.ReadKey()
End Sub
End Module
3
Explanation of the Code
Imports System
This is called the namespace declaration. What we are doing is that we are including a namespace
with the name System into our programming structure. After that, we will be able to access all the
methods that have been defined in that namespace without getting an error.
Module Program
This is called a module declaration. Here, we have declared a module named Program. VB.Net is
an object-oriented language. Hence we must have a class module in every program. It is inside this
module that we will be able to define the data and methods to be used by the program.
Sub Main()
A VB.Net module or class can have more than one procedures. It is inside procedures where we
should define our executable code. This means that the procedure will define the class behavior. A
procedure can be a Function, Sub, Get, Set, AddHandler, Operator, RemoveHandler, or RaiseEvent.
In this line, we defined the Main sub-procedure. This marks the entry point in all VB.Net programs.
It defines what the module will do when it is executed.
’This is a simple VB.Net Program to print hello world in console
This is a comment. To mark it as a comment, we added a single quote (‘) to the beginning of the
sentence. The VB.Net compiler will not process this part. The purpose of comments is to improve the
readability of the code. Use them to explain the meaning of various statements in our code. Anyone
reading through our code will find it easy to understand.
Console.WriteLine("Hello World!")
This is where we have specified the behavior of the primary method. The WriteLine method belongs
to the Console class, and it is defined inside the System namespace. Remember this was imported
into the code. This statement makes the program print the text ”Hello World!” on the console when
executed.
Console.ReadKey()
This line will prevent the screen from closing or exiting soon after the program has been executed.
The screen will pause and wait for the user to perform an action to close it.
End Sub
Closing the main sub-procedure.
End Module
Ending the module.
4
Output of the program
Class in VB.Net
In VB.Net, we use classes to define a blueprint for a Data Type. It does not mean that a class definition
is a data definition, but it describes what an object of that class will be made of and the operations
that we can perform on such an object. Objects are instances of a class. The methods and variables
that constitute a class are called members of the class. In VB.Net, to define a class, we use the Class
keyword, which should be followed by the name of the class, the class body, and the End Class
statement. This is described in the following syntax:
[ <attributelist> ] [ accessmodifier ] _
Class name
[ Inherits classname ]
[ statements ]
End Class
Here,
• The attributeList denotes a list of attributes that are to be applied to the class. This is optional
in class declaration .
• accessmodifier defines the access levels of the class, it has values as - Public, Protected, Friend,
Protected Friend and Private. This is optional in class declaration .
• The Inherits denotes any parent class that it inherits.
Example
Imports System
Module Module1
Class Figure
Public length As Double
Public breadth As Double
End Class
Sub Main()
Dim Rectangle As Figure = New Figure()
Dim area As Double = 0.0
5
Rectangle.length = 8.0
Rectangle.breadth = 7.0
area = Rectangle.length * Rectangle.breadth
Console.WriteLine("Area of Rectangle is : {0}", area)
Console.ReadKey()
End Sub
End Module
Explanation of the code
Class Figure
Creating a class named Figure.
Public length As Double
Public breadth As Double
Creating a class member named length and breadth of type Double. Its access level has been set to
public meaning that it will be accessed publicly.
End Class
Ending the class.
Dim Rectangle As Figure = New Figure()
Creating an object named Rectangle. This object will be of type Figure, meaning that it will be
capable of accessing all the members defined inside the Figure class.
Dim area As Double = 0.0
Defining a variable named area of type Double and initializing its value to 0.0.
Rectangle.length = 8.0
Rectangle.breadth = 7.0
Accessing the length and breadth property defined in the Figure class and initialize its value to 8.0
and 7.0 respectively.
area = Rectangle.length * Rectangle.breadth
Calculating the area of the rectangle by multiplying the values of length and breadth. The result of
this calculation will be assigned to the area variable.
Output of the program
6
Structure in VB.Net
A structure is a user-defined data type. Structures provide us with a way of packaging data of different
types together. A structure is declared using the Structure keyword.
Example of Structure in VB.Net
Module Module1
Structure Struct
Public x As Integer
Public y As Integer
End Structure
Sub Main()
Dim st As New Struct
st.x = 10
st.y = 20
Dim sum As Integer = st.x + st.y
Console.WriteLine("The result is {0}", sum)
Console.ReadKey()
End Sub
End Module
Code Explanation
Structure Struct
Creating a structure named Struct.
Public x As Integer
Public y As Integer
Creating a variable x and y of type integer. The access level has been set to Public to make it publicly
accessible.
End Structure
End of the structure.
Dim st As New Struct
Creating an object named st of type Struct. This means that it will be capable of accessing all the
properties defined within the structure named Struct.
st.x = 10
st.y = 20
Accessing the variables x and y defined within the structure Struct and initializing its value to 10 and
20 respectively.
Dim sum As Integer = st.x + st.y
Defining the variable sum and initializing its value to the sum of the values of the above two variables.
7
Output of the program