Give this program code:

using VikingErik.NetMF.Collections;
using VikingErik.NetMF.MicroLinq;
...
...

namespace VikingErik.NetMF.Hub
{
public class Program
{
public class ComplicatedClass1
{
public string Prop1 { get; private set; }

public ComplicatedClass1(string value)
{
Prop1 = value;
}
}

public class ComplicatedClass2 : IComparable
{
public string Prop2 { get; private set; }

public ComplicatedClass2(string value)
{
Prop2 = value;
}

public int CompareTo(object obj)
{
if (obj is ComplicatedClass2)
return this.Prop2.CompareTo(((ComplicatedClass2)obj).Prop2);

throw new ArgumentException();
}
}

public static void Main()
{
Thread.Sleep(1000);
OutputPort onboardLed = new OutputPort(Pins.ONBOARD_LED, true);

NearlyGenericArrayList Complicated1 = new NearlyGenericArrayList(typeof(ComplicatedClass1));
Complicated1.Add(new ComplicatedClass1("Carrot"));
Complicated1.Add(new ComplicatedClass1("Apple"));
Complicated1.Add(new ComplicatedClass1("Cucumber"));
Complicated1.Add(new ComplicatedClass1("Banana"));

NearlyGenericArrayList Complicated2 = new NearlyGenericArrayList(typeof(ComplicatedClass2));
Complicated2.Add(new ComplicatedClass2("Grenadine"));
Complicated2.Add(new ComplicatedClass2("Eggplant"));
Complicated2.Add(new ComplicatedClass2("Gummy Bears"));
Complicated2.Add(new ComplicatedClass2("Froyo"));

NearlyGenericArrayList stringList = new NearlyGenericArrayList(typeof(string));
stringList.Add("4");
stringList.Add("2");
stringList.Add("7");
stringList.Add("1");
stringList.Add("9");
stringList.Add("3");
stringList.Add("8");
stringList.Add("6");
stringList.Add("5");


Debug.Print(string.Empty);
Debug.Print("Complicated1 Contains:");
foreach (var s in Complicated1)
{
Debug.Print(((ComplicatedClass1)s).Prop1);
}

Debug.Print(string.Empty);
Debug.Print("Complicated2 Contains:");
foreach (var s in Complicated2)
{
Debug.Print(((ComplicatedClass2)s).Prop2);
}

Debug.Print(string.Empty);
Debug.Print("StringList Contains:");
foreach (var s in stringList)
{
Debug.Print((string)s);
}

if (Complicated1.CanBeSorted)
{
Debug.Print(string.Empty);
Debug.Print("Complicated1 can be sorted. Sorting...");
Complicated1.Sort();
Debug.Print("Complicated1 Contains:");
foreach (var s in Complicated1)
{
Debug.Print(((ComplicatedClass1)s).Prop1);
}
}

if (Complicated2.CanBeSorted)
{
Debug.Print(string.Empty);
Debug.Print("Complicated2 can be sorted. Sorting...");
Complicated2.Sort();
Debug.Print("Complicated2 Contains:");
foreach (var s in Complicated2)
{
Debug.Print(((ComplicatedClass2)s).Prop2);
}
}

if (stringList.CanBeSorted)
{
Debug.Print(string.Empty);
Debug.Print("StringList can be sorted. But String doesn't implement IComparable! Sorting...");
stringList.Sort();
Debug.Print("StringList Contains:");
foreach (var s in stringList)
{
Debug.Print((string)s);
}
}

if (Complicated1.Any(item => ((ComplicatedClass1)item).Prop1.Substring(0, 1).Equals("A")))
{
Debug.Print(string.Empty);
Debug.Print("Found an item starting with A.");
for (int i = 0; i < 10; i++)
{
onboardLed.Write(false);
Thread.Sleep(100);
onboardLed.Write(true);
Thread.Sleep(100);
}
}


Debug.Print(string.Empty);
Debug.Print(DateTime.Now.TimeOfDay.ToString());
Debug.Print("Complicated1 Couldn't use built-in Sort so let's try OrderBy extension...");
var res = Complicated1.OrderBy(p => ((ComplicatedClass1)p).Prop1);
foreach (var o in res)
{
Debug.Print(((ComplicatedClass1)o).Prop1);
}
Debug.Print(DateTime.Now.TimeOfDay.ToString());
Debug.Print(string.Empty);



while (true)
{
onboardLed.Write(false);
Thread.Sleep(5000);
onboardLed.Write(true);
Thread.Sleep(250);
}
}
}
}



The Debugger Output shows the following:

Complicated1 Contains:
Carrot
Apple
Cucumber
Banana

Complicated2 Contains:
Grenadine
Eggplant
Gummy Bears
Froyo

StringList Contains:
4
2
7
1
9
3
8
6
5

Complicated2 can be sorted. Sorting...
Complicated2 Contains:
Eggplant
Froyo
Grenadine
Gummy Bears

StringList can be sorted. But String doesn't implement IComparable! Sorting...
StringList Contains:
1
2
3
4
5
6
7
8
9

Found an item starting with A.

18:55:51.3358098
Complicated1 Couldn't use built-in Sort so let's try OrderBy extension...
Apple
Banana
Carrot
Cucumber
18:55:51.3408108

Last edited Feb 18, 2011 at 1:00 AM by VikingErik, version 2

Comments

No comments yet.