Showing posts with label Generics. Show all posts
Showing posts with label Generics. Show all posts

Tuesday, 13 May 2014

Generics in asp.net

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace Test_n.G
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List names = new List();
names.Add("A");
names.Add("B");
names.Add("C");
lboxStrings.DataSource = names;
lboxStrings.DataBind();
string temp1 = names[2];
Customer c1 = new Customer("1", "A", "AA", "123-4567");
Customer c2 = new Customer("2", "B", "BB", "123-7823");
Customer c3 = new Customer("3", "C", "CC", "123-6383");
List customers = new List();
customers.Add(c1);
customers.Add(c2);
customers.Add(c3);
Customer c = customers[2];
customers[1] = c;
lboxCustomers.DataSource = customers;
lboxCustomers.DataBind();
Dictionary dict = new Dictionary();
dict.Add(c1.Id, c1);
dict.Add(c2.Id, c2);
lboxDictionary.DataSource = dict;
lboxDictionary.DataBind();
}
}
public abstract class AbstractEntity
{
private string _id;
public AbstractEntity(string id)
{
_id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract bool IsValid
{
get;
}
}
public class Customer : AbstractEntity
{
private string _firstName;
private string _lastName;
private string _phone;
public Customer(string id, string firstName, string lastName, string phone)
: base(id)
{
_firstName = firstName;
_lastName = lastName;
_phone = phone;
}
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
public string Name
{
get { return LastName + ", " + FirstName; }
}
public override bool IsValid
{
get
{
if (Id.Length > 0 && LastName.Length > 0)
return true;
else
return false;
}
}
public override string ToString()
{
return Id + "," + Name + "," + Phone;
}
}
}
--------------------
asp:ListBox ID="lboxStrings" runat="server"
asp:ListBox ID="lboxCustomers" runat="server"
DataTextField="Name"
DataValueField="Id"
asp:ListBox ID="lboxDictionary" runat="server"
DataTextField="Value"
DataValueField="Key"
----------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace Test_narayana.G
{
public partial class A : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Author A1 = new Author(TextBox1.Text, TextBox2.Text, TextBox3.Text, "123-4567");
Author A2 = new Author("TTT", "B", "BB", "123-7823");
Author A3 = new Author("MS", "C", "CC", "123-6383");
Dictionary dict = new Dictionary();
dict.Add(A1.Id, A1);
dict.Add(A2.Id, A2);
dict.Add(A3.Id, A3);
lboxDictionary.DataSource = dict;
lboxDictionary.DataBind();
ReadDic(dict);
}
protected void ReadDic(Dictionary dict)
{
//Read Dictionary
foreach (KeyValuePair kv in dict)
{
Response.Write(kv.Key + ":" + kv.Value);
Author au = kv.Value;
Response.Write(au.Id);
Response.Write(au.ItemURL);
Response.Write(au.AuthorName);
Response.Write(au.searchKeyWord);
}
}
}
public abstract class AAbstractEntity
{
private string _id;
public AAbstractEntity(string id)
{
_id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract bool IsValid
{
get;
}
}
public class Author : AAbstractEntity
{
private string _ItemURL;
private string _AuthorName;
private string _searchKeyWord;
public Author(string id, string ItemURL, string AuthorName, string searchKeyWord)
: base(id)
{
_ItemURL = ItemURL;
_AuthorName = AuthorName;
_searchKeyWord = searchKeyWord;
}
public string ItemURL
{
get { return _ItemURL; }
set { _ItemURL = value; }
}
public string AuthorName
{
get { return _AuthorName; }
set { _AuthorName = value; }
}
public string searchKeyWord
{
get { return _searchKeyWord; }
set { _searchKeyWord = value; }
}
//public string Name
//{
// get { return AuthorName + ", " + ItemURL; }
//}
public override bool IsValid
{
get
{
if (Id.Length > 0 && AuthorName.Length > 0)
return true;
else
return false;
}
}
//public override string ToString()
//{
// return Id + "," + Name + "," + searchKeyWord;
//}
public override string ToString()
{
return Id + "," + AuthorName + ", " + ItemURL + "," + searchKeyWord;
}
}
}

Tuesday, 29 April 2014

C# Generics Demo

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
public partial class GenericsDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Student dhas = new Student("Manick", "Dhas", 22);
Student raj = new Student("Sundar", "Raj", 32);
///Using a custom strongly typed StudentList
StudentList mc = new StudentList();
mc.Add(dhas);
mc.Add(raj);
Response.Write("Using a custom strongly typed StudentList
");
foreach (Student s in mc)
{
Response.Write("First Name : " + s.FirstName + "
");
Response.Write("Last Name : " + s.LastName + "
");
Response.Write("Age : " + s.Age + "

");
}
///Creating a list of Student objects using my custom generics
MyCustomList student = new MyCustomList();
student.Add(dhas);
student.Add(raj);
Response.Write("
Using a list of Student objects using my custom generics
");
foreach (Student s in student)
{
Response.Write("First Name : " + s.FirstName + "
");
Response.Write("Last Name : " + s.LastName + "
");
Response.Write("Age : " + s.Age + "

");
}
///Creating a list of Student objects using my custom generics
MyCustomList intlist = new MyCustomList();
intlist.Add(1);
intlist.Add(2);
Response.Write("
Using a list of String values using my custom generics
");
foreach (int i in intlist)
{
Response.Write("Index : " + i.ToString() + "
");
}
///Creating a list of Student objects using my custom generics
MyCustomList strlist = new MyCustomList();
strlist.Add("One");
strlist.Add("Two");
Response.Write("
Using a list of int values using my custom generics
");
foreach (string str in strlist)
{
Response.Write("Index : " + str + "
");
}
}
}
///
/// Student Class
///
public class Student
{
private string fname;
private string lname;
private int age;
///
/// First Name Of The Student
///
public string FirstName
{
get { return fname; }
set { fname = value; }
}
///
/// Last Name Of The Student
///
public string LastName
{
get { return lname; }
set { lname = value; }
}
///
/// Age of The Student
///
public int Age
{
get { return age; }
set { age = value; }
}
///
/// Creates new Instance Of Student
///
/// FirstName
/// LastName
/// Age
public Student(string fname, string lname, int age)
{
FirstName = fname;
LastName = lname;
Age = age;
}
}
///
/// Strongly Typed Class
/// Accepts only Student Type
///
public class StudentList : IEnumerable
{
private ArrayList alist = new ArrayList();
///
/// Adds new value of type Student
///
/// Object of Student Class
/// Returns the index of the Object
public int Add(Student value)
{
try
{
return alist.Add(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes The Object from StudentList
///
/// Object of Student Class
public void Remove(Student value)
{
try
{
alist.Remove(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes the student object
///
/// Index of the Student object to be removed
public void RemoveAt(int index)
{
try
{
alist.RemoveAt(index);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Gets the count of the StudentList
///
public int Count
{
get { return alist.Count; }
}
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a StudentList.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the StudentList
public IEnumerator GetEnumerator()
{
try
{
return alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
///
/// Custom Generic Accepts any Types
///
///
public class MyCustomList : IEnumerable, IEnumerable
{
private ArrayList alist = new ArrayList();
///
/// Adds new value of type T
///
/// Object of Type T
/// Returns the index
public int Add(T value)
{
try
{
return alist.Add(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes The Object from MyCustomList
///
/// Object of Type T
public void Remove(T value)
{
try
{
alist.Remove(value);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Removes the object at index
///
/// Index of the T object to be removed
public void RemoveAt(int index)
{
try
{
alist.RemoveAt(index);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// Gets the count of the MyCustomList Object
///
public int Count
{
get { return alist.Count; }
}
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the collection
public IEnumerator GetEnumerator()
{
try
{
return alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region IEnumerable Members
///
/// Returns an enumerator that iterates through a collection.
///
/// An System.Collections.IEnumerator object that can be used to iterate through the collection
IEnumerator IEnumerable.GetEnumerator()
{
try
{
return (IEnumerator)alist.GetEnumerator();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}