Events are similar to properties.
Both of them are declared to be of certain type,which in case of an event it is forced to be delegate type,The reason for having events in the first place is very much like the reason for having properties.
Coding Clues
/*Microsoft dot net Programming*/
Wednesday, August 10, 2011
Tuesday, August 9, 2011
Difference between Delegate and MulticastDelegate?
What is delegate?
A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.
What is Multicast delegates?
Multicast delegates nothing more than normal delegates that have multiple method references in their invocation list
Note:delegate and multicast delegates are not two types of delegates it is the way we are implementing delegates if we assigned only one method it is single cast if more methods its multicast like wise.
A delegate is a type that references a method. Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.
What is Multicast delegates?
Multicast delegates nothing more than normal delegates that have multiple method references in their invocation list
Note:delegate and multicast delegates are not two types of delegates it is the way we are implementing delegates if we assigned only one method it is single cast if more methods its multicast like wise.
Monday, February 21, 2011
ADO.NET - Generic Programming Model
We are usually writing code for handling databases, specific to any database like sql server, oracle etc. how we can write code with out specifying the database or code that suite for any database provider?. yes it is possible by using the class DbProviderFactory.
lets see how we can use dbproviderfatory to make database layer for our program.
for example :if we are using sql server and c# then our database specific code will be like this
//NameSpace
using System.Data.SqlClient;
//Code
SqlConnection con = new SqlConnection();
con.ConnectionString = "Your Connection String";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Your Command Text";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
we can write a database specific code for mysql like this
//NameSpace
using MySql.Data .MySqlClient ;
//Code
MySqlConnection con = new MySqlConnection();
con.ConnectionString = "Your Connection String";
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "Your Command Text";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
how we can generalize our code, by using dbproviderfatory . to better understand this class you should have an idea about provider factory and abstract factory design pattern.
Just read articles in references for more info
ok. then how we can implement this:
//NameSpace
using System.Data.Common;
//Code
DbProviderFactory oProviderFactory;
DbConnection con;
oProviderFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
//oProviderFactory = DbProviderFactories.GetFactory("System.Data.MySqlClient") 4 MySQL
Con = oProviderFactory.CreateConnection;
Con.ConnectionString = "Your Connection String";
DbCommand cmd = con.CreateCommand();
cmd.CommandText ="Your Command Text";
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
It is always better to go for generic programming model you can easily switch to any database later.
References:
Generic Coding with the ADO.NET 2.0 Base Classes and Factories
Writing Generic Data Access Code in ASP.NET 2.0 and ADO.NET 2.0
Abstract Factory Design Pattern in ADO.NET 2.0
It is always better to go for generic programming model you can easily switch to any database later.
References:
Generic Coding with the ADO.NET 2.0 Base Classes and Factories
Writing Generic Data Access Code in ASP.NET 2.0 and ADO.NET 2.0
Abstract Factory Design Pattern in ADO.NET 2.0
Tuesday, February 9, 2010
Check Printer Status Using VB.NET
Some times our application needs to show the status of the printer, for that purpose we can use System.Management Namespace for getting printer status
'btnStatus is a button.
'tbPrinterStatus is a rich text box.
Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatus.Click
Dim scope As ManagementScope = New ManagementScope("\root\cimv2")
scope.Connect()
Dim searcher As ManagementObjectSearcher = New ManagementObjectSearcher("SELECT * FROM Win32_Printer")
Dim collection As ManagementObjectCollection = searcher.Get
For Each printer As ManagementObject In collection
'MessageBox.Show(printer("WorkOffline").ToString(), cbPrinters.Text)
If printer("Name").ToString() = cbPrinters.Text Then
tbPrinterStatus.Clear()
If printer("WorkOffline").ToString().ToLower().Equals("true") Then
tbPrinterStatus.AppendText("Printer : Offline")
Else
tbPrinterStatus.AppendText("Printer : Online")
End If
tbPrinterStatus.AppendText(vbCrLf)
tbPrinterStatus.AppendText("PrinterStatus :")
tbPrinterStatus.AppendText(printer("PrinterStatus").ToString())
tbPrinterStatus.AppendText(vbCrLf)
tbPrinterStatus.AppendText("ExtendedDetectedErrorState :")
tbPrinterStatus.AppendText(printer("ExtendedDetectedErrorState").ToString())
tbPrinterStatus.AppendText(vbCrLf)
tbPrinterStatus.AppendText("DetectedErrorState :")
tbPrinterStatus.AppendText(printer("DetectedErrorState").ToString())
tbPrinterStatus.AppendText(vbCrLf)
tbPrinterStatus.AppendText("PrinterState :")
tbPrinterStatus.AppendText(printer("PrinterState").ToString())
End If
Next
End Sub
Note: By Using this code you will never get exact printer status,it will display the status from the printer drivers not from device .
Subscribe to:
Comments (Atom)