|
Sorting Ints, Strings, etc
|
ArrayList list = ...
list.Sort();
|
|
Sorting—Custom Objects
|
public void sortMe()
{
DateSort ds = new DateSort();
myDateList.Sort(ds);
}
public class DateSort : IComparer
{
public DateSort()
{
//
// TODO: Add constructor logic here
//
}
public int Compare(object x, object y)
{
DateTime dx = (DateTime) x;
DateTime dy = (DateTime) y;
if (dx.Date == dy.Date)
{
return 0; //equal!
}
else if (dx.Date < dy.Date)
{
//list dx before dy
return -1;
}
else
{
//list dx after dy
return 1;
}
}
public int CompareTo(object y)
{
return Compare(this,y);
}
}
|
|
Overriding ToString()
|
public class Message
{
public override string ToString()
{
return “Subject is: “ + this.Subject;
}
}
|
|
Checking type of object
|
object o = ...
if (o is int)
{
int val = (int) o;
}
|
|
|
Exception Handling
|
try
{
//I hope things go ok...
}
catch (System.IO.IOException ioex)
{
MessageBox.Show(ex.Message, "Disk stuff”);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Uh oh...");
}
finally
{
//clean up, close the file, etc
}
|
|
Operator Overloading
|
public static Gayle operator +(Gayle m1, Gayle m2)
{
//now we can call m1 + m2
return new Gayle(m1.name, m2.name);
}
|
|
Enumerations
|
public enum Justification
{
Left,
Right,
Center
};
public bool isLeft()
{
if (this.alignment == Justification.Left)
{
return true;
}
return false;
}
|
|
Get and Set
|
public static int MinimumAge
{
get
{
return minimum_age;
}
set
{
if(value < 0)
{
minimum_age = 0;
}
else
{
  minimum_age = value;
}
}
}
|
|