VB.Net Tutorial for Beginners
Visual Basic is an easy-to-learn programming language that is type-safe. Get the fundamentals to gain expertise in web development through this VB.Net tutorial.
Introduction to VB.Net
An object-oriented programming language called Visual Basic.NET (VB.NET) is used to create .net programs, including Windows, Web, and mobile apps. We cover the following in this VB.Net tutorial:
- Overview of VB.Net
- Environment Setup
- VB.Net Basic Syntax
- Data Types in VB.Net
- Variables in VB.Net
- Classes and Objects in VB.Net
- Advantages of VB.Net
Overview of VB.Net
VB.net facilitates the creation of various online, client-server, and mobile apps. Microsoft’s .NET framework is used to implement VB.NET.
It can access every library in the .Net Framework to the fullest extent possible. VB.NET programs can also be executed on Mono, the open-source substitute for .NET, on Linux or Mac OS X in addition to Windows. Check out our DotNet course syllabus.
Features of VB.Net
VB.Net is a professional language that is extensively used for the reasons listed below.
- Easy to learn.
- Object-oriented.
- Structured language.
- It produces efficient programs.
- Modern and general-purpose.
- Component oriented.
- Part of .Net Framework.
- Numerous computer platforms are capable of compiling it.
Explore what our dotnet full-stack job seeker program has in store for your career.
Environment Setup
With the revolutionary .Net framework, you may develop the following kinds of applications:
- Windows applications
- Web applications
- Web services
The apps built with the.Net framework are cross-platform. The framework can be utilized with Visual Basic, C#, C++, Jscript, COBOL, and other languages because of the way it has been developed. Learn the computer fundamentals through our C and C++ training in Chennai.
- Each of these languages can interact with the framework and with other languages.
- The client languages, such as VB.Net, leverage a vast library of codes that make up the.Net framework. These languages follow an object-oriented approach.
Some of the components of VB.Net:
- The .Net Framework Class Library
- Common Language Runtime (CLR)
- Common Language Specification
- Windows Forms
- Common Type System
- ASP.Net and ASP.Net AJAX
- Metadata and Assemblies
- ADO.Net
- Windows Workflow Foundation (WF)
- Windows Communication Foundation (WCF)
- Windows Presentation Foundation
- LINQ
Integrated Development Environment (IDE) For VB.Net
The following development tools are offered by Microsoft for VB.Net programming:
- Visual Studio 2010 (VS)
- Visual Basic 2010 Express (VBE)
- Visual Web Developer
You can create a wide range of VB.Net applications with these tools, from straightforward command-line programs to intricate ones.
Writing VB.Net Programs on Linux or Mac OS
While the Windows operating system is the primary platform for the .NET Framework, there exist alternate versions that are compatible with other operating systems.
- Mono is a free and open-source variant of the.NET Framework that functions on multiple platforms, such as Linux and Mac OS and comes with a Visual Basic compiler.
- VB 2012 is the most recent version.
Accelerate your career with our advanced dotnet training in Chennai.
VB.Net Basic Syntax
VB.Net is an object-oriented programming language. A program using the object-oriented programming paradigm is made up of different objects that communicate with one another through actions.
- Object: An object is a specific instance of a class. Objects possess states and actions.
- An example of a dog’s state would be its color, breed, and behaviors, such as its wagging, barking, eating, etc.
- Class: A class is an outline or template that specifies the states and behaviors that objects of that type are capable of supporting.
- Methods: A method is a behavior. Numerous methods can be found in a class. It is in the procedures that all of the activities are carried out, data is altered, and logic is expressed.
- Instance Variables: Every object possesses a distinct collection of instance variables. An object’s state is determined by the values that are assigned to these instance variables.
Example: Rectangle Class in VB.Net
Let us have a look at a rectangle object as an example. It has dimensions such as width and length. Depending on the design, methods for showing details, computing area, and accepting the values of various attributes may be required.
Imports System
Public Class Rectangle
Private length As Double
Private width As Double
‘Public methods
Public Sub AcceptDetails()
length = 4.5
width = 3.5
End Sub
Public Function GetArea() As Double
GetArea = length * width
End Function
Public Sub Display()
Console.WriteLine(“Length: {0}”, length)
Console.WriteLine(“Width: {0}”, width)
Console.WriteLine(“Area: {0}”, GetArea())
End Sub
Shared Sub Main()
Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
End Class
Output
Length: 4.5
Width: 3.5
Area: 15.75
To create things, you utilize classes. For instance, r is a rectangle object in the code.
Dim r As New Rectangle()
If indicated, a class may contain members who are reachable from outside the classroom. Fields are members of a data set, whereas methods are members of a procedure.
It is possible to call static or shared methods without first establishing a class object. Instance methods are called using a class object:
Shared Sub Main()
Dim r As New Rectangle()
r.Acceptdetails()
r.Display()
Console.ReadLine()
End Sub
Our ASP.NET training in Chennai covers everything that a developer needs to learn.
Data Types in VB.Net
An elaborate framework for declaring variables or functions of various sorts is referred to as data types. A variable’s type affects both how much storage space it takes up and how the bit pattern it stores is interpreted.
A vast array of data types are available in VB.Net. Some of the popular data types are as follows:
- Integer (4 bytes): -2,147,483,648 through 2,147,483,647 (signed)
- Char (2 bytes): 0 through 65535 (unsigned)
- Boolean: True or False
- Byte (1 byte): 0 to 255
- String: 0 to approximately 2 billion Unicode characters
- Single (4 bytes): -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values.
- User-Defined (depends on the platform being used): Every component of the structure has a range that is distinct from the ranges of the other components and is based on the type of data it contains.
Example
Module DataTypes
Sub Main()
Dim b As Byte
Dim n As Integer
Dim si As Single
Dim d As Double
Dim da As Date
Dim c As Char
Dim s As String
Dim bl As Boolean
b = 1
n = 1234567
si = 0.12345678901234566
d = 0.12345678901234566
da = Today
c = “U”c
s = “Me”
If ScriptEngine = “VB” Then
bl = True
Else
bl = False
End If
If bl Then
‘the oath taking
Console.Write(c & ” and,” & s & vbCrLf)
Console.WriteLine(“declaring on the day of: {0}”, da)
Console.WriteLine(“Learn VB.Net at SLA”)
Console.WriteLine(“Considering the outcome for the floating point variables:”)
Console.WriteLine(“The Single: {0}, The Double: {1}”, si, d)
End If
Console.ReadKey()
End Sub
End Module
Output
U and, Me
declaring on the day of: 10/10/2024 08:00:00 PM
Learn VB.Net at SLA
Considering the outcome for the floating point variables:
The Single:0.1234568, The Double: 0.123456789012346
The Type Conversion Functions in VB.Net
The following are some of the in-line type conversion routines that are available in VB.Net:
- CBool(expression): The expression is converted to a Boolean data type.
- CByte(expression): It transforms the expression into a data type of byte.
- CChar(expression): The expression is converted to the Char data type.
- CDate(expression): The expression is converted to the Date data type.
- CDbl(expression): The expression is converted to the Double data type.
- CDec(expression): The expression is converted to a Decimal data type.
- CInt(expression): The expression is converted to an integer data type.
- CLng(expression): The expression is converted to the Long data type.
Example
Module DataTypes
Sub Main()
Dim n As Integer
Dim da As Date
Dim bl As Boolean = True
n = 1234567
da = Today
Console.WriteLine(bl)
Console.WriteLine(CSByte(bl))
Console.WriteLine(CStr(bl))
Console.WriteLine(CStr(da))
Console.WriteLine(CChar(CChar(CStr(n))))
Console.WriteLine(CChar(CStr(da)))
Console.ReadKey()
End Sub
End Module
Output
True
-1
True
10/10/2024
1
1
Reshape your career with our C-Sharp Training in Chennai.
Variable Declaration in VB.Net
Variable declaration and storage allocation for one or more variables are handled by the Dim statement. At the module, class, structure, function, or block level, the Dim statement is utilized.
Syntax:
[ < attributelist > ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]
[ ReadOnly ] Dim [ WithEvents ] variablelist
- attributelist: The attributes that apply to the variable are listed in the attributelist. Optional.
- accessmodifier: The values of this variable, Public, Protected, Friend, Protected Friend, and Private, indicate the various access levels. Optional.
- Shared: By using the “shared” declaration, a shared variable is created that is accessible to all instances of the class or structure and is not linked to any particular instance. Optional.
- Shadows: Shadows show that a base class variable re-declares and obscures a group of overloaded elements, or an identically named element. Optional.
- Static: The term “static” denotes that the variable will hold its value even after the declared method has ended. Optional.
- ReadOnly: The variable is marked as ReadOnly if it can only be read, not written. Optional
- WithEvents: The variable is used to respond to events raised by the instance that is assigned to it, as specified by the WithEvents parameter. Optional.
- Variablelist: The list of declared variables is provided by variablelist.
The syntax for declaring a variable:
variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]
- variablename: It is the variable’s name.
- boundslist (optional): It gives an array variable’s boundaries for each dimension.
- New (optional): The Dim statement launches a new instance of the class.
- Datatype: necessary when Option Strict is enabled. The variable’s data type is specified.
- initializer: Optional. if New is not given. When a variable is created, it is assigned an expression that is evaluated.
Example
Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date
Dim pi As Double
pi = 3.14159
Considering the User’s Values
A function called ReadLine is available in the System namespace’s Console class to take user input and save it in a variable.
Syntax
Dim message As String
message = Console.ReadLine
Example
Module variablesNdataypes
Sub Main()
Dim message As String
Console.Write(“Enter message: “)
message = Console.ReadLine
Console.WriteLine()
Console.WriteLine(“Your Message: {0}”, message)
Console.ReadLine()
End Sub
End Module
R- and L-values
Two categories of expressions exist:
- lvalue: An expression that represents a lvalue can occur on the left or right side of an arrangement.
- rvalue: An expression that is a rvalue can be found on an assignment’s right side but not its left.
Because they are lvalues, variables can be found on the left side of an assignment. Since numeric literals are rvalues, they cannot be assigned values or appear on the left side of the screen.
Example
Dim g As Integer = 20
Get the fundamental understanding with our Dot Net training courses in Chennai.
Classes and Objects in VB.Net
The term “Class” appears first in a class definition, then the class name, the class body, and the End Class statement. The general format for a class definition is as follows:
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ MustInherit | NotInheritable ] [ Partial ] _
Class name [ ( Of typelist ) ]
[ Inherits classname ]
[ Implements interfacenames ]
[ statements ]
End Class
A Box class with three data members, length, width, and height, is shown in the example below.
Module mybox
Class Box
Public length As Double ‘ Length of a box
Public breadth As Double ‘ Breadth of a box
Public height As Double ‘ Height of a box
End Class
Sub Main()
Dim Box1 As Box = New Box() ‘ Declare Box1 of type Box
Dim Box2 As Box = New Box() ‘ Declare Box2 of type Box
Dim volume As Double = 0.0 ‘ Store the volume of a box here
‘ box 1 specification
Box1.height = 5.0
Box1.length = 6.0
Box1.breadth = 7.0
‘ box 2 specification
Box2.height = 10.0
Box2.length = 12.0
Box2.breadth = 13.0
‘volume of box 1
volume = Box1.height * Box1.length * Box1.breadth
Console.WriteLine(“Volume of Box1 : {0}”, volume)
‘volume of box 2
volume = Box2.height * Box2.length * Box2.breadth
Console.WriteLine(“Volume of Box2 : {0}”, volume)
Console.ReadKey()
End Sub
End Module
Output
Volume of Box1 : 210
Volume of Box2 : 1560
Finetune your app development skills with our MVC training in Chennai.
Member Functions and Encapsulation
A function that has its prototype or definition included in the class declaration, just like any other variable, is called a member function of a class.
It has access to all other members of a class for that object and can operate on any object within the class of which it is a member.
From a design standpoint, member variables are characteristics of an object that are kept secret to achieve encapsulation. It is only possible to access these variables using the public member functions.
Example
Module mybox
Class Box
Public length As Double ‘ Length of a box
Public breadth As Double ‘ Breadth of a box
Public height As Double ‘ Height of a box
Public Sub setLength(ByVal len As Double)
length = len
End Sub
Public Sub setBreadth(ByVal bre As Double)
breadth = bre
End Sub
Public Sub setHeight(ByVal hei As Double)
height = hei
End Sub
Public Function getVolume() As Double
Return length * breadth * height
End Function
End Class
Sub Main()
Dim Box1 As Box = New Box() ‘ Declare Box1 of type Box
Dim Box2 As Box = New Box() ‘ Declare Box2 of type Box
Dim volume As Double = 0.0 ‘ Store the volume of a box here
‘ box 1 specification
Box1.setLength(6.0)
Box1.setBreadth(7.0)
Box1.setHeight(5.0)
‘box 2 specification
Box2.setLength(12.0)
Box2.setBreadth(13.0)
Box2.setHeight(10.0)
‘ volume of box 1
volume = Box1.getVolume()
Console.WriteLine(“Volume of Box1 : {0}”, volume)
‘volume of box 2
volume = Box2.getVolume()
Console.WriteLine(“Volume of Box2 : {0}”, volume)
Console.ReadKey()
End Sub
End Module
Output
Volume of Box1 : 210
Volume of Box2 : 1560
Advantages of VB.Net
Among the many benefits of Visual Basic.NET (VB.NET) are the following:
- Database connectivity: Database connections such as SQL Server, MySQL, and Oracle can be made with VB.NET.
- Security features: To improve application security, VB.NET comes with built-in security capabilities, including code access security.
- Interoperability: Developers may operate within the same application with other .NET languages, such as C# and F#, thanks to VB.NET.
- Development environment: Code debugging, IntelliSense, and a visual designer are just a few of the VB.NET development tools available in Microsoft’s integrated development environment (IDE), Visual Studio.
- Community and resources: VB.NET boasts a sizable developer community with a wealth of online resources, including libraries, forums, documentation, and tutorials.
- Game Development: Using GUI tools, game developers can write gaming software in VB.NET.
Learn to develop apps with our wide range of software training courses at SLA.
Conclusion
We have covered the basic concepts in this VB.Net tutorial. Enrich your development skills with our VB.Net training in Chennai.