vb.net
vb.net
EXCEPTION HANDLING
An Exception is an unwanted event that interrupts the normal flow of the program
execution . When an exception occurs program execution gets terminated.
Exception handling is one of the most important feature of java programming that
allows us to handle the runtime errors caused by exceptions.
If an exception occurs, which has not been handled by programmer then program
execution gets terminated and a system generated error message is shown to the user
Exception handling ensures that the flow of the program doesn’t break when an
exception occurs.
In the .Net Framework, exceptions are represented by classes. The exception classes
in .Net Framework are mainly directly or indirectly derived from the
System.Exception class. Some of the exception classes derived from the
System.Exception class are the System.ApplicationException and
System.SystemException classes.
The System.SystemException class is the base class for all predefined system
exception
Handling Exceptions
VB.Net provides a structured solution to the exception handling problems in the
form of try and catch blocks.
============================================================================
ADO.NET
ADO.NET is a data access technology from the Microsoft .NET Framework .
ADO.NET is a module of .Net Framework which is used to establish connection between
application and data sources. Data sources can be such as SQL Server and XML.
ADO.NET is sometimes considered an evolution of ActiveX Data Objects (ADO)
technology. ADO.NET consists of classes that can be used to connect, retrieve,
insert and delete data.
ADO.NET provides a bridge between the front end controls and the back end database.
It is commonly used by programmers to access and modify data stored in relational
database systems, though it can also access data in non-relational data source.
All the ADO.NET classes are located into System.Data.dll .
ADO.NET has two main components that are used for accessing and manipulating data
are the .NET Framework data provider and the DataSet.
=====================================================
ADO.NET Framework Data Providers
Data provider is used to connect to the database, execute commands and retrieve the
record. It is lightweight component with better performance. It also allows us to
place the data into DataSet to use it further in our application.
The .NET Framework provides the following data providers that we can use in our
application.
==.NET Framework Data Provider for SQL Server It provides data access for
Microsoft SQL Server. It requires the System.Data.SqlClient namespace.
==.NET Framework Data Provider for OLE DB It is used to connect with OLE DB. It
requires the System.Data.OleDb namespace.
==.NET Framework Data Provider for ODBC It is used to connect to data sources by
using ODBC. It requires the System.Data.Odbc namespace.
==.NET Framework Data Provider for Oracle It is used for Oracle data sources. It
uses the System.Data.OracleClient namespace.
ADO.NET CONNECTION
The Connection Object is a part of ADO.NET Data Provider
The ADO.NET Connection Object is used for establishing a connection between data
source and application.. Through this connection, you can access and manipulate a
database.
ADO.NET Connection Object connect to the specified Database and open a connection
between the application and the Database . When the connection is established , SQL
Commands may be executed, with the help of the Connection Object, to retrieve or
manipulate data in the Database. Once the Database activity is over , Connection
should be closed
In ADO.NET the type of the Connection is depend on what Database system you are
working with. E.G
SqlConnection
OleDbConnection
OdbcConnection
OleDBConnectionclass is used for a database like an oracle and MS-Access.
SqlConnection class is used for the MS-SQL database.
ADO.NET COMMAND
The Command Object in ADO.NET is used to executes SQL command/statements (Query)
like (Select * from Student).
The Command Object required an instance of a Connection Object for executing the
SQL statements.
ADO.NET DATAREADER
DataReader Object in ADO.NET is a stream-based WHICH IS used to read or retrieve
data from database to application,which do not update the data.The DataReader
cannot be created directly from code, they created only by calling the
ExecuteReader method of a Command Object.
There are two types of DataReader in ADO.NET. They are SqlDataReader and the
OleDbDataReader. The System.Data.SqlClient and System.Data.OleDb are containing
these DataReaders respectively.
ADO.NET DATASET
It is a collection of data tables where datatable is a collection or represention
of the relational data in tabular form. It is used to fetch data without
interacting with a Data Source that's why, it also known as disconnected data
access method.
It is an in-memory data store that can hold more than one table at the same time.
It contains a copy of the original database tables.
ADO.NET provides a DataSet class that can be used to create DataSet object. It
contains constructors and methods to perform data related operations.
It gives a better advantage over DataReader , because the DataReader is working
only with the connection oriented Data Sources.
ADO.NET DATAADAPTER
DataAdapter provides the communication between the Dataset and the Data Source with
the help of Connection Object .DataAdapter works as a bridge between a DataSet and
a data source to retrieve data FROM database to dataset.The Connection Object has
no information about the data it retrieves . Similarly a Dataset has no knowledge
of the Data Source where the data coming from. So the DataAdapter manage the
communication between these two Objects.
DataAdapter is a class that represents a set of SQL commands and a database
connection. It can be used to fill the DataSet and update the data source
ADO.NET DataTable
DataTable represents relational data into tabular form. ADO.NET provides a
DataTable class to create and use data table independently. It can also be used
with DataSet also. Initially, when we create DataTable, it does not have table
schema. We can create table schema by adding columns and constraints to the table.
After defining table schema, we can add rows to the table.
===========================================================
SHADOWING
The main purpose of shadowing is to protect the definition of your class members.
Shadowing happens by default when two members share the same name but override
keyword is not mentioned. However you can explicitly perform shadowing using a
keyword.
n VB.NET, shadowing is done using shadows keyword
================================================================
NAMESPACING
VB.NET namespaces are used by most programmers is to tell the compiler which .NET
Framework libraries are needed for a particular program. .NET codes can be
organized using namespaces
Namespaces are use to avoid conflicts between classes that have same names. For ex:
We can use two classes with same names but with different namespaces.
Namespaces helps to create logical groups of related classes & Interfaces that can
be used by any language target by .Net framework.
System is the basic namespace used by every .NET code. For example, System.IO,
System.Net,
"Import System", where Imports is a statement that inherit the system namespace. A
System is a namespace that contains basic classes, reference data types, events,
attributes, and various inbuilt functions that help to run the program.
==================================================================
DELEGATS
A delegate is a class that can hold a reference to a method. Unlike other classes,
a delegate class has a signature, and it can hold references only to methods that
match its signature.
Delegates are similar to function pointers in C, C++.
When you are passing Function as parameter, it is known as Delegate.
All the events like mouseclick, keypress, etc., handle some delegate in .NET.
=Declaration Delegate is like a normal Procedure/Method in VB.NET which accepts a
parameter but doesn’t return Sub in case of a Sub method and type in case of a
Function.
=Delegate does not return any type value.
=Delegates have only declaration, they do not have any body.
Public Delegate Sub SomeWorkDelegate(ByVal strSomeString As String)
=To use a Delegate, you need to attach some function to it. Delegates on their own
do not do anything.
Why do we need delegates in ?
Programmers often needs to pass a method as a parameter of other methods. For this
purpose we create and use delegates.
Events in VB.NET are handled by delegates,
The keyword structure can be used to declare a structure in vb.net. The general
form of a structure declaration in VB.NET is as follows.
Within the body of the structure declaration, the individual structure members (or
member variables) are declared using a series of Dim statements. The statement End
Structure marks the end of the structure declaration.
The individual members of a structure can be accessed by using the dot (.) operator
as s
howing below.
ms.x = 10
ms.y = 20
==inside a struct, we can only declare a field. We can't initialize a field inside
a struct. However we can use constructor to initialize the structure fields.
However a struct can contain static fields, which can be initialized inside the
struc
A VB.NET structure can also contain methods. The methods can be either static or
non-static. But static methods can access only other static members and they can't
invoke by using an object of the structure. They can invoke only by using the
struct name.
The operators can be overloaded inside a VB.NET structure also. The same rules
applicable with respect to a VB.NET class is also applicable here. Both unary and
binary operators can be overloaded.
PROGRAM
Imports System
Structure MyStruct
Public x As Integer
Public y As Integer
End Structure 'MyStruct
Class MyClient
Public Shared Sub Main()
Dim msAs New MyStruct
ms.x = 10
ms.y = 20
Dim sumAs Integer = ms.x + ms.y
Console.WriteLine("The sum is {0}", sum)
End Sub 'Main
End Class 'MyClient
Structures and classes differ in the following particulars:
===================================================================================
==
STRING
A string is nothing but a collection of characters. In very simple terms, String
may be defined as the array of characters,a string is the data type that allows the
storage of the set of characters in a single variable
The string is a very useful data type in any programming language.
String: It is the keyword that is used to declare string in vb.net.
Below is the syntax to declare a string in VB .net.
STRING Function
String functions are used in computer programming languages to manipulate a string
or query information about a string (some do both).
The name of the string class is System.String that contains the various function of
string.
1. Asc function
This string function in VB.Net is used in order to get the integer value of the
first letter of the string. Its integer value is actually the integer value of that
character.
2.Mid Function
Mid() in Visual Basic.net is a string function that is used to extract a copy of a
character or set of characters from a string.
In the above syntax Str specifies the string from which the characters to be
extracted from the starting position specified using Start, until the specifed
Length of characters.
Example:
Module Module1
Sub Main()
Dim Str As String = "Seven Wonders of the World"
Console.WriteLine("It is a beautiful" & Mid(Str, 21, 6))
Console.ReadLine()
End Sub
End Module
======================================================================
What is a SubString?
The substring function is used to obtain a part of a specified string. This method
is defined in the String class of Microsoft VB.NET. You have to specify the start
index from which the String will be extracted. The String will be extracted from
that index up to the length that you specify. Substring function accepts two
arguments
If we pass only one argument to the function, The function will copy all the data
in the String that begins from that index
We can use the Substring function to get a single character from a string.
Syntax of Substring
The function accepts two arguments as shown in the following syntax:
Module Module1
Sub Main()
Console.ReadKey()
End Sub
End Module
==mdi vs substring
=both mid and substring performs the same type of operation on the string i.e. to
extract a part of the string .but the basic difference lies,how they indices the
string characters.
in case you hace a string "test" the mdi funtion will indice the characters as
1,2,3,4 whereas substring indice the character as 0,1,2,3.
===================================================================================
===
in how many ways function can return value in vb.net and what is stream?
Function Returning a Value
In VB.Net, a function can return a value to the calling code in two ways −
The stream is basically the sequence of bytes passing through the communication
path. There are two main streams: the input stream and the output stream. The input
stream is used for reading data from file (read operation) and the output stream is
used for writing into the file (write operation).
The Stream class in .NET is an abstract class that is located in the System.IO
namespace
. When a file is opened for reading or writing, it becomes a stream.
==========================================================================
CONSTRUCTOR=constructor is a special member Sub of a class
In visual basic.NET, Constructor is a SPECIAL type of member sub and it will
invoke automatically whenever an instance of class or structure is created. The
constructor is useful to initialize and set default values for the data members of
the new object.
In case, if we create a class without having any constructor, the compiler will
automatically create a one default constructor for that class. So, there is always
one constructor that will exist in every class.
In visual basic, a class can contain more than one constructor with a different
type of arguments . the constructors will never return anything, so we don’t need
to use any return type,.
by default constructors are of Public access type.
' Constructor
End Sub
End Class
TYPES
DEFAULT
PARAMETERIZED ==
Class Line
Private length As Double ' Length of a line
Public Sub New(ByVal len As Double) 'parameterised constructor
Console.WriteLine("Object is being created, length = {0}", len)
Console.ReadKey()
End Sub
End Class
Copy Constructor
Private Constructor=As the name implies a private constructor is a constructor
having the access level defined as private,This means that (in most of the
scenarios) an instance of the class cannot be created outside the class using this
constructor
===============================================================================
Panel control vs Group box control
2 The group box control cannot display the scroll bar. The panel control can
display the scroll bar.
3 Only limited number of controls can be placed within the group box.Any number of
controls can be placed within the panel.
4 Group box is a light weight component.Panel is a heavy weight component.
========================================================================
VB.NET TextBox Control
A TextBox control is used to display, accept the text from the user as an input,.
or a single line of text on a VB.NET Windows form at runtime ,but Images and other
multimedia content are not supported. Furthermore, we can add multiple text and
scroll bars in textbox control. However, we can set the text on the textbox that
displays on the form.
==========================================================
VB.NET Label Control
In VB.NET, a label control is used to display text in control for the user. It
does not participate in user input or keyboard or mouse events. Also, we cannot
rename labels at runtime. The labels are defined in the class System.Windows.Forms
namespace.
==SizeMode
Determines the size of the image to be displayed in the control. This property
takes its value from the PictureBoxSizeMode enumeration, which has values −
==PROGRAM
Public Class Picturebx
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Sub
End Class
=====================================
ARRAY
An array is a collection of items stored at contiguous memory locations. The idea
is to store multiple items of the same type together.
Each data item is called an element of the array.
Declaration of VB.NET Array
We can declare an array by specifying the data of the elements followed by
parentheses () in the VB.NET.
Next →← Prev
VB.NET Arrays
An array is a linear data structure that is a collection of data elements of the
same type stored on a contiguous memory location. Each data item is called an
element of the array. It is a fixed size of sequentially arranged elements in
computer memory with the first element being at index 0 and the last element at
index n - 1, where n represents the total number of elements in the array.
VB.NET Arrays
In the above diagram, we store the Integer type data elements in an array starting
at index 0. It will continue to store data elements up to a defined number of
elements.
OR
Next →← Prev
VB.NET Dynamic Array
A Dynamic array is used when we do not know how many items or elements to be
inserted in an array. To resolve this problem, we use the dynamic array. It allows
us to insert or store the number of elements at runtime in sequentially manner. A
Dynamic Array can be resized according to the program's requirements at run time .
You can declare a dynamic array using the ReDim statement.
IN OTHERS WORDS
Dynamic arrays are arrays that can be dimensioned and re-dimensioned as par the
need of the program.
Where,
The Preserve keyword helps to preserve the data in an existing array, when you
resize it.
Imports System
Module Dynamic_arr1
Sub Main()
'Declaration and Initialization of String Array Days()
Dim Days() As String
' Resize an Array using the ReDim Statement
ReDim Days(2)
Days(0) = "Sunday"
Days(1) = "Monday"
Days(2) = "Tuesday"
Console.WriteLine(" Before Preserving the Elements")
For i As Integer = 0 To Days.Length - 1
Console.WriteLine("Days Name in [{0}] index is {1}", i, Days(i))
Next
Console.WriteLine()
Multidimensional Array
In VB.NET, a multidimensional array is useful for storing more than one dimension
in a tabular form, such as rows and columns. The multidimensional array support two
or three dimensional in VB.NET.