예제 #1
0
        // Have to implement the CompareTo function because Droid implements IDroid, and IDroid implements IComparable
        public int CompareTo(Object obj)
        {
            // If the obj that is passed in is null, just return 1. Unable to compare
            if (obj == null)
            {
                return(1);
            }

            // Cast the obj to a droid.
            Droid droid = (Droid)obj;

            // Check to see if the droid is null. Perhaps the cast will fail and droid will be null.
            if (droid != null)
            {
                // Do the comparison. Rather than doing the work of returning -1, 0, or 1
                // I just delegated the work to the CompareTo method of decimal
                return(this.TotalCost.CompareTo(droid.TotalCost));
            }
            // Else, the droid is null, so the droid that was passed in must not have been a droid.
            // Throw an exception stating so.
            else
            {
                throw new ArgumentException("Object is not a Droid");
            }
        }
예제 #2
0
        // CompareTo Method
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            Droid otherDroid = obj as Droid;

            if (otherDroid != null)
            {
                return(this.totalCost.CompareTo(otherDroid.totalCost));
            }
            else
            {
                throw new ArgumentException("Object is not a Droid");
            }
        }
예제 #3
0
        // CompareTo method required by IComparable
        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return(1);
            }

            // Get second element
            Droid otherDroid = obj as Droid;

            // Go on to compare if second element is not null
            if (otherDroid != null)
            {
                // Calculate total cost for both elements and compare them
                otherDroid.CalculateTotalCost();
                this.CalculateTotalCost();
                return(totalCost.CompareTo(otherDroid.totalCost));
            }
            else
            {
                throw new ArgumentException("Object is not a Droid");
            }
        }