Tuesday, January 6, 2009

USE of GENRIC CLASSES

-------------USE GENRIC CLASSES-----------------
Mny friedns ask me how to use Genric in our application, and what is genric.
so second question is on your part what is genric. i m giving two simple but
quite important example of genric which suerly make u understand how to use
genric.

Generic Type
Generics introduce a flexibility that combines type safety with the ability to avoid committing
to a type at design time.
What
Generics enable class, delegate, interface, and struct types, and methods to be created with
type parameters that are placeholder types, which can be substituted when the type is known.
Where
Generics are commonly used in data structures—for example, collections and arrays, or in
passing method parameters.
 
Why
Generics overcome the overhead issues of casting and boxing between types, which adversely
affects performance.
How
Generics are specified within a pair of delimiters ("<" and ">") placed after the respective type
or method name. When the type is known, it is substituted for the generic placeholder, as the
following code snippet illustrates. Note that the method
GenericMethod is declared as generic,
as is its parameter. In the Main function, we vary the types of the method.
using System;
using System.Collections.Generic;
using System.Text;
namespace ModelT
{
public class ModelTGenerics
{
public static void GenericMethod(T arg)
{
Console.WriteLine("Calling Model T generic method, " + arg.GetType());
}
public static void Main()
{
//Call the generic method passing different types:
GenericMethod("Hallo Generics");
GenericMethod(16.75);
}
}
}
 
The Standard: Generic Type
The standard acknowledges the use of generics to reduce overhead and increase type
flexibility while retaining the protection of type safety.


In First example we pass two different type in single genric class.
In second example we get data with help of genric class.

First Example :-

add new page in ur test application and paste following code

protected void Page_Load(object sender, EventArgs e)
{
//create a string version of our generic class
Col mystring = new Col();
//set the value
mystring.Val = "hello";

//output that value
Response.Write("Value :- " + mystring.Val+"
");


//output the value's type
Response.Write("Type :- " + mystring.Val.GetType() + "
");

//create another instance of our generic class, using a different type
Col myint = new Col();
//load the value
myint.Val = 5;
//output the value
Response.Write("Value :- "+ myint.Val + "
");
//output the value's type
Response.Write("Type :- " + myint.Val.GetType() + "
");

}
public class Col
{
T t;
public T Val { get { return t; } set { t = value; } }
}



There are a few things to notice. The class name "Col" is our first indication that this Type is generic, specifically the

brackets containing the Type placeholder. This Type placeholder "T" is used to show that if we need to refer to the actual

Type that is going to be used when we write this class, we will represent it as "T". Notice on the next line the variable

declaration "T t;" creates a member variable with the type of T, or the generic Type which we will specify later during

construction of the class (it will actually get inserted by the Common Language Runtime (CLR) automatically for us). The

final item in the class is the public property. Again, notice that we are using the Type placeholder "T" to represent that

generic type for the type of that property. Also notice that we can freely use the private variable "t" within the class.

This example is form
and there is more example for genric on this page.

Second Example :-

In this example we get data with help of genric class

Step 1.
caeate a simple class as following.

using System;

///
/// Summary description for UserInfo
///

public class UserInfo
{
private string _userName = string.Empty;
private string _password = string.Empty;


public UserInfo(string userName, string password)
{
this._userName = userName;
this._password = password;
}

public string UserName
{
get
{
return _userName;
}
}

public string Password
{
get
{
return _password;
}
}
}

Step 2.

create genric class which uses its type as a above class

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Data.SqlClient;

///
/// Summary description for genricList
///

public class genricList
{
public genricList()
{

}
public System.Collections.Generic.IList fillIlist()
{
string st = ConfigurationManager.ConnectionStrings[0].ToString();
IList userinfos = new List();
string connectionString = ConfigurationManager.ConnectionStrings["PUBSConnectionString"].ToString();
string queryString = "SELECT * FROM GenricLogin";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText = queryString;

try
{
connection.Open();

SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
UserInfo info = new UserInfo(reader[0].ToString(),reader[1].ToString());
userinfos.Add(info);

}
reader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

return userinfos;
}
}

table structure

CREATE TABLE GenricLogin (username varchar(20), password varchar(20))

Insert some values in table

Insert into GenricLogin (username,password)

select 'aaaaa','111111'
union all
select 'bb','222222222'
union all
select 'ccc','33333333'


Step 3

now call this funciton on page and bind your dataGrid with genric

genricList objgenricList = new genricList();
dg.DataSource = objgenricList.fillIlist();
dg.DataBind();


with same way we can add as much classes as we required for bind different tables.


Thanks
HelpOnDesk Team