Example #1
0
        // Once	the	BankAccount	has	been	made	to	implement	the	IComparable interface,	you	can	use	the	Sort	behaviors	provided	by
        // collection	types.
        public void Comparables()
        {
            //	Create	20	accounts	with	random	balances
            List <IAccount> accounts = new     List <IAccount>();
            Random          rand     = new     Random(1);

            for (int i = 0; i < 20; i++)
            {
                IAccount account = new BankAccountComparable(rand.Next(0, 10000));
                accounts.Add(account);
            }
            //	Sort	the	accounts
            accounts.Sort();
        }
Example #2
0
            public int CompareTo(object obj)
            {
                // if we are comparaed with lower object:
                if (obj == null)
                {
                    return(1);
                }
                //	Convert	the	object	reference	into	an	account	reference
                BankAccountComparable bank = obj as BankAccountComparable;

                //	as	generates	null	if	the	conversion	fails
                if (bank == null)
                {
                    throw new Exception("Not an account");
                }
                //	use	the	balance	value	as	the	basis	of	the	comparison
                throw new NotImplementedException();
            }