Professional Documents
Culture Documents
IV
IV
V.B-PART-IV
Handles
The Handles keyword is used to declare that a particular procedure will handle a specific event. By using Handles keywords with a
procedure declaration, we can enable the procedure to handle events that were raised by an object variable declared using the
WithEvent keyword. The withEvents statement and Handles clause provide a declarative way of specifying event handlers.
Class x
Public event A
Public sub test()
RaiseEvent A
End sub
End class
Module m
Dim withEvents obj as x
Public sub main()
Obj= new x()
Obj.Test()
End sub
Public sub OurHandler Handles obj.A
System.Console.WriteLine(Event is being handled )
End sub
End module
Delegates
The term delegates is defined as a representative of a country, organization or another person. In VB. Net, a delegate is defined as
a representative of a function or a procedure. It holds a reference to a method.
A delegate declaration must specify the parameters and / or the return type for the method which it represents.
VB. Net allows us to create two type of Delegate classes single cast delegates and multi cast delegates. Single cast delegates are
delegate classes that are derived from the Delegate class. Multicast delegates are derived from the Multicast Delegate class.
These delegates typically have an invocation list or linked list of delegates that are executed when the invoke method is called.
Single cast delegates contain invocation lists with only one method, whereas multicast delegates contain invocation lists with
more than one method.
Delegate function isdivisible(n as integer)
Module m
Public function check (I as integer)
If I mod 5 = 0 then
System.Console.WriteLine(Divisible )
Else
System.Console.WriteLine(not Divisible)
End if
End function
Public sub main()
Dim x as indivisible
Page 1
ArguS academy
V.B-PART-IV
X=addressof check
X(11)
End sub
End module
Inheriting event
A derived class inherits events from its base class. It is perfectly valid to write a separate handler for the event in the derived class.
This can be done using the Handles keyword with the MyBase keyword followed by the event name.
Class x
Public event p
Public sub test()
RaiseEvent p
End sub
End class
Class y
Inherits x
Public sub DerivedHandler Handles MyBase.p
System.Console.WriteLine(Base class Event is being handled in derived class)
End sub
End class
Module m
Dim withEvents obj as y
Public sub main()
obj = new y()
obj.Test()
end sub
end module
Page 2
ArguS academy
V.B-PART-IV
Module m
Public sub DoAction()
Dim i as Integer=1
While true
System.Console.WriteLine(i)
System.Console.WriteLine(Thread is running )
i=i+1
end while
end sub
public sub main()
dim t as new System.Threading.Thread(AddressOf DoAction)
t.start()
end sub
end module
Declares a DoAction() method that will serve as the starting point for the thread. This method declares an integer variable and
prints the incremented value of this variable each time within an infinite loop.
Sleep method
Module m
Public sub DoAction()
Dim i as integer=1
While true
System.Console.WriteLine(i)
System.Console.WriteLine(Thread is running)
System.Threading.Thread.sleep(400)
i=i+1
end while
end sub
public sub main()
dim t as new System.Threading.Thread(AddressOf DoAction)
t.Start()
end sub
end module
The suspend() method of the System.Threading.Thread class is used to suspend the action of a thread for an indefinite period. The
thread can be resumed using the Resume() method.
Module m1
Dim t1 as new System.Threading.Thread(Addressof DoAction)
Dim t2 as new System.Threading.Thread(Addressof DoAction)
Public sub DoAction()
Dim I as integer=1
While true
System.Console.WriteLine(i)
i=i+1
if i=4 then
System.Console.WriteLine(t1 is now going to be suspended )
System.Threading.Thread.CurrentThread.Suspend()
End if
System.Threading.Thread.sleep(200)
End while
End sub
Public sub DoAction2()
Dim j as integer=100
While true
System.Console.WriteLine(j)
System.Threading.Thread.Sleep(200)
End while
End sub
Public sub main()
t1.Start()
t2.Start()
end sub
end module
Page 3
ArguS academy
V.B-PART-IV
Synchronization
When two or more threads in a multithread application access a common method or data simultaneously, there will be conflict
and as a result vital information could be lost or data will become inconsistent. To avoid this, it is necessary to have some kind of
mechanism in a multithreaded application that allows the methods or data to be accessed by one thread at a time.
Synchronization is a process that allows threads to access methods or data one thread at a time.
Module m
Dim a1 as new A()
Dim a2 as new A()
Dim t1 as new System.Threading.Thread(AddressOf a1.Test)
Dim t2 as new System.Threading.Thread(AddressOf a2.Test)
Public i as integer =1
Public sub main()
t1.Start()
t2.Start()
end sub
end module
class A
public sub Test()
while true
System.Console.WriteLine(i)
i=i+1
System.Threading.Thread.Sleep(200)
End while
End sub
End class
Page 4
ArguS academy
V.B-PART-IV
Dim a as new x
A=Nothing
System.GC.collection()
System.GC.WaitPendingFinalizers()
System.Console.WriteLine(End of Main)
End sub
End module
Class x
Public sub new
System.Console.WriteLine(Constructor)
End sub
Protected Overrides sub Finalize
System.Console.WritelINE(Destructors)
End sub
End class
Introduction to ADO.NET
Traditionally, an application maintained database connection as long as it executed. This is not feasible as open connection
consume valuable resources. Also, performance is reduced because of the overheads in maintaining connection.
ADO.NET is a data object model that lets us work with databases version of ADO designed for the .Net framework. Data access in
ADO.Net is based on disconnected architecture. This means that the connection need not be maintained continuously while the
application is executing. The application connects to the database as and when it need to retrieve and update data.
Maintainability: It is difficult to increase the number of application tiers within an application after the application has
been deployed. However, if the application is implemented using ADO.Net then increasing the number of tiers becomes a
very easy process.
Programmability: Data classes in ADO.Net result in typed dataset and thus allows us to access data through typed
programming. This marks code easier to read, write and debug. It is also safer because it provides for type checking at
compile at compile time.
Performance: As stated earlier, Ado.NET works with disconnected architecture. This increases performance.
Interoperability : Since ADO.Net makes extensive use of XML, interoperability becomes very simple using ADO.Net
Command Object
The Command objects are used to retrieve and manipulate data. For this purpose, sqlDataAdapter and OleDbAdapter
class are defined in the System.Data.SqlClient and System.Data.OleDb namespace respectively;
Page 5
ArguS academy
V.B-PART-IV
DataSet
While working with database, applications often need to fetch sets of data repeatedly. In a disconnected architecture it is not
feasible for the application to go back to the database every time data needs to be fetched. It is much more feasible to have a
temporary storage of records from where the application can fetch data whenever needs. A dataset is one such temporary
storage. It is a cache of records fetched from the database. The dataset class in the System.Data namespace is the main building
block of
Imports Microsoft.visualbasic
Imports System
Imports System.data
Imports System.XML
Module m
Sub Main()
Dim myconn As new
Data.SqlClient.SqlConnection (server=Ritcha\sqldb;database=EmpDep;uid=sa;password=passwd;)
Myconn.open()
Dim adap As New Data.SqlClient.SqlDataAdapter(s, myconn)
Dim dataset As Data. Dataset=New Data.dataSet()
Adap.fill(dataset, details)
Dimtbl as datatable= dataset.tables(details)
Dim dr As DataRow
Dim I As DataRow
Dim j As Datacolumn
For Each I In tbl.Rows
For Each j In tbl.columns
System.Console.Write((i(j).ToString()).PadRight(15,))
Next
System.Console.WriteLine()
Next
Myconn.close()
End Sub
End Module
We create a database connection usi9ng the SqlConnection class. We pass the name of the database server, the name of the
database and pass it an SQL command along with the connection name. The SQL command retrieves all the records from the
department table. We fill the results of this command into a dataset and extract the dataset rows and columns using a nested for
loop. We print each of these details on the console within the nested for loop. In order to format the data while printing. We use
the PadRight() function to include appropriate spaces in-between the columns. Finally, when all the records have been retrieved
and printed, we close the connection.
Page 6