Last Minute Revision
ASP.net Technologies
Unit - II

VB.NET Overview

VB.NET (Visual Basic .NET) is a programming language developed by Microsoft. It is built on the .NET framework and allows for building a wide range of applications, including web, desktop, and mobile apps.

1. Introduction to VB.NET

VB.NET is a high-level programming language that supports object-oriented programming (OOP) principles. It is known for its ease of use and readability, making it suitable for beginners and advanced developers alike.


2. VB.NET Syntax Essentials

Statements

  • Statements in VB.NET are the building blocks of a program. They perform actions like declaring variables, calling methods, and controlling the flow of execution.

Example:

Dim num As Integer = 10
Console.WriteLine(num)

Lines and Comments

  • Lines: Each statement usually occupies one line in VB.NET. A statement can be split across multiple lines using an underscore (_).
  • Comments: Used to explain code. Comments in VB.NET are preceded by an apostrophe (').

Example:

' This is a single-line comment
Dim name As String = "VB.NET"

3. Operators in VB.NET

Operators in VB.NET allow for performing operations on variables and values. Common types include:

  • Arithmetic Operators: +, -, *, /
  • Comparison Operators: =, <>, >, <, >=, <=
  • Logical Operators: And, Or, Not

Example:

Dim result As Integer = 5 + 3 ' result = 8

4. Procedures

In VB.NET, procedures are blocks of code that perform specific tasks. There are two main types:

  • Sub Procedures: Perform actions without returning a value.
  • Function Procedures: Perform actions and return a value.

Example:

Sub ShowMessage()
    Console.WriteLine("Hello, VB.NET!")
End Sub
 
Function AddNumbers(a As Integer, b As Integer) As Integer
    Return a + b
End Function

5. Variables and Constants

Variables

  • Variables store data that can be modified during the execution of a program. Variables can be declared implicitly (without specifying the type) or explicitly (with a type).

Implicit Declaration:

Dim myVar = 10 ' VB.NET infers it as Integer

Explicit Declaration:

Dim myVar As Integer = 10

Constants

  • Constants store values that do not change during program execution.

Example:

Const Pi As Double = 3.14159

6. Arrays

An array in VB.NET is a collection of variables that share the same data type.

Example:

Dim numbers(5) As Integer ' Declares an array of 6 elements
numbers(0) = 10

7. Branching and Looping

Branching

  • If-Else Statement: Used to make decisions in the code.

Example:

If num > 10 Then
    Console.WriteLine("Greater than 10")
Else
    Console.WriteLine("Less than or equal to 10")
End If

Looping

  • For Loop, While Loop, and Do-While Loop: Used to repeat a block of code.

Example:

For i As Integer = 1 To 5
    Console.WriteLine(i)
Next

Object-Oriented Programming in VB.NET

1. Objects and Classes

In VB.NET, a class is a blueprint for creating objects. A class defines properties, methods, and events that objects of that class will have.

Example:

Class Car
    Public Property Model As String
    Public Sub New(modelName As String)
        Model = modelName
    End Sub
End Class
 
Dim myCar As New Car("Toyota")
Console.WriteLine(myCar.Model)

2. Inheritance

Inheritance allows a class to inherit properties and methods from another class. In VB.NET, the keyword Inherits is used for this purpose.

Example:

Class Vehicle
    Public Sub Start()
        Console.WriteLine("Vehicle started")
    End Sub
End Class
 
Class Car
    Inherits Vehicle
End Class
 
Dim myCar As New Car()
myCar.Start() ' Inherits Start() method from Vehicle

3. Accessibility of Inherited Properties and Methods

Inherited members (properties and methods) can have different levels of accessibility:

  • Public: Accessible from anywhere.
  • Protected: Accessible from the class itself and any derived classes.
  • Private: Only accessible within the class.

4. Overriding Methods

Overriding allows a subclass to provide a specific implementation of a method that is already defined in its parent class.

Example:

Class Vehicle
    Public Overridable Sub Start()
        Console.WriteLine("Vehicle started")
    End Sub
End Class
 
Class Car
    Inherits Vehicle
    Public Overrides Sub Start()
        Console.WriteLine("Car started")
    End Sub
End Class

System Class

1. Working with Numbers

The System.Math class provides methods for performing mathematical operations like rounding, finding the square root, etc.

Example:

Dim result As Double = Math.Sqrt(16) ' result = 4

2. Manipulating Strings

The System.String class provides methods for string manipulation like concatenation, trimming, and searching.

Example:

Dim fullName As String = "John" & " " & "Doe"

3. DateTime Arithmetic

You can perform arithmetic operations on dates using the DateTime class.

Example:

Dim today As DateTime = DateTime.Now
Dim tomorrow As DateTime = today.AddDays(1)

4. Converting and Formatting Values

The Convert class is used to convert between different data types.

Example:

Dim str As String = "123"
Dim num As Integer = Convert.ToInt32(str)

Namespace and Assemblies

1. Namespaces

A namespace is a way to organize code and avoid name conflicts. Namespaces group related classes, functions, and types together.

Example:

Namespace Vehicles
    Class Car
        ' Class definition
    End Class
End Namespace

2. Assemblies

An assembly is a compiled code library used by .NET applications. It can be an EXE or DLL file.


3. Relating Namespaces and DLL Assemblies

  • DLLs (Dynamic Link Libraries) contain reusable code that can be shared between multiple applications. Each DLL contains one or more namespaces.

4. Creating and Importing Assemblies

Creating Assemblies

  • You can create assemblies by compiling a class library project in VB.NET.

Importing Assemblies

  • Assemblies can be imported into a VB.NET project using the Imports statement.

Example:

Imports System.Text

5. Using Imported Assemblies

Imported assemblies provide access to pre-built classes and functions.


6. Compiling with Imported Namespaces

When compiling a VB.NET project, the compiler uses the imported namespaces to resolve class and method references. This allows you to utilize functionality provided by external libraries and frameworks.