Due to heavy rain and humidity, very tiny water bubbles formed in the surface of my Pears soap, in the light it was looking like some kind of precious stone.
Dec 3, 2008
My Soap
Due to heavy rain and humidity, very tiny water bubbles formed in the surface of my Pears soap, in the light it was looking like some kind of precious stone.
Nov 19, 2008
ArrayList Sorting
using System;
using System.Collections;
public class Person : IComparable
{
#region Private Members
private string _firstname;
private string _lastname;
private int _age;
#endregion
#region Properties
public string Firstname
{
get { return _firstname; }
set { _firstname = value; }
}
public string Lastname
{
get { return _lastname; }
set { _lastname = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
#endregion
#region Contructors
public Person(string firstname, string lastname, int age)
{
_firstname = firstname;
_lastname = lastname;
_age = age;
}
#endregion
#region ToString()
public override string ToString()
{
return String.Format("{0} {1}, Age = {2}", _firstname,
_lastname, _age.ToString());
}
#endregion
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is Person)
{
Person p2 = (Person)obj;
return _firstname.CompareTo(p2._firstname);
}
else
throw new ArgumentException("Object is not a Person.");
}
#endregion
public int CompareTo(Person p2, PersonComparer.ComparisonType comparisonMethod)
{
switch (comparisonMethod)
{
case PersonComparer.ComparisonType.Lastname:
return _lastname.CompareTo(p2._lastname);
case PersonComparer.ComparisonType.Age:
return _age.CompareTo(p2._age);
case PersonComparer.ComparisonType.Firstname:
default:
return _firstname.CompareTo(p2._firstname);
}
}
#region PersonComparer
public class PersonComparer : IComparer
{
public enum ComparisonType
{ Firstname = 1, Lastname = 2, Age = 3 }
private ComparisonType _comparisonType;
public ComparisonType ComparisonMethod
{
get { return _comparisonType; }
set { _comparisonType = value; }
}
#region IComparer Members
public int Compare(object x, object y)
{
Person p1;
Person p2;
if (x is Person)
p1 = x as Person;
else
throw new ArgumentException("Object is not of type Person.");
if (y is Person)
p2 = y as Person;
else
throw new ArgumentException("Object is not of type Person.");
return p1.CompareTo(p2, _comparisonType);
}
#endregion
}
#endregion
}
public class TestClass
{
public static void Main()
{
// Create ArrayList
ArrayList people = new ArrayList();
people.Add(new Person("John", "Doe", 76));
people.Add(new Person("Abby", "Normal", 25));
people.Add(new Person("Jane", "Doe", 84));
// Create Person Comparer Class
Person.PersonComparer comparer = new Person.PersonComparer();
// Sort By Lastname
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Lastname;
people.Sort(comparer);
Console.WriteLine("Last Name");
TestClass.DisplayPeople(people);
// Sort By Age
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Age;
people.Sort(comparer);
Console.WriteLine("Age");
TestClass.DisplayPeople(people);
// Sort By Firstname
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Firstname;
people.Sort(comparer);
Console.WriteLine("First Name");
TestClass.DisplayPeople(people);
}
public static void DisplayPeople(ArrayList people)
{
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
}
}
using System.Collections;
public class Person : IComparable
{
#region Private Members
private string _firstname;
private string _lastname;
private int _age;
#endregion
#region Properties
public string Firstname
{
get { return _firstname; }
set { _firstname = value; }
}
public string Lastname
{
get { return _lastname; }
set { _lastname = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
#endregion
#region Contructors
public Person(string firstname, string lastname, int age)
{
_firstname = firstname;
_lastname = lastname;
_age = age;
}
#endregion
#region ToString()
public override string ToString()
{
return String.Format("{0} {1}, Age = {2}", _firstname,
_lastname, _age.ToString());
}
#endregion
#region IComparable Members
public int CompareTo(object obj)
{
if (obj is Person)
{
Person p2 = (Person)obj;
return _firstname.CompareTo(p2._firstname);
}
else
throw new ArgumentException("Object is not a Person.");
}
#endregion
public int CompareTo(Person p2, PersonComparer.ComparisonType comparisonMethod)
{
switch (comparisonMethod)
{
case PersonComparer.ComparisonType.Lastname:
return _lastname.CompareTo(p2._lastname);
case PersonComparer.ComparisonType.Age:
return _age.CompareTo(p2._age);
case PersonComparer.ComparisonType.Firstname:
default:
return _firstname.CompareTo(p2._firstname);
}
}
#region PersonComparer
public class PersonComparer : IComparer
{
public enum ComparisonType
{ Firstname = 1, Lastname = 2, Age = 3 }
private ComparisonType _comparisonType;
public ComparisonType ComparisonMethod
{
get { return _comparisonType; }
set { _comparisonType = value; }
}
#region IComparer Members
public int Compare(object x, object y)
{
Person p1;
Person p2;
if (x is Person)
p1 = x as Person;
else
throw new ArgumentException("Object is not of type Person.");
if (y is Person)
p2 = y as Person;
else
throw new ArgumentException("Object is not of type Person.");
return p1.CompareTo(p2, _comparisonType);
}
#endregion
}
#endregion
}
public class TestClass
{
public static void Main()
{
// Create ArrayList
ArrayList people = new ArrayList();
people.Add(new Person("John", "Doe", 76));
people.Add(new Person("Abby", "Normal", 25));
people.Add(new Person("Jane", "Doe", 84));
// Create Person Comparer Class
Person.PersonComparer comparer = new Person.PersonComparer();
// Sort By Lastname
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Lastname;
people.Sort(comparer);
Console.WriteLine("Last Name");
TestClass.DisplayPeople(people);
// Sort By Age
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Age;
people.Sort(comparer);
Console.WriteLine("Age");
TestClass.DisplayPeople(people);
// Sort By Firstname
comparer.ComparisonMethod = Person.PersonComparer.ComparisonType.Firstname;
people.Sort(comparer);
Console.WriteLine("First Name");
TestClass.DisplayPeople(people);
}
public static void DisplayPeople(ArrayList people)
{
foreach (Person p in people)
Console.WriteLine(p);
Console.ReadLine();
}
}
Subscribe to:
Posts (Atom)