Subject: Beginning Visual C# Exercises - Chapter 11 Answers
Posted By: seblake Post Date: 9/24/2004 4:15:55 PM
1.    ... Add to the library with the Person class
        public class People : DictionaryBase
        {
            public void AddPerson(Person newPerson)
            {
                Dictionary.Add(newPerson.Name, newPerson);
            }

            public void RemovePerson(string keyName)
            {
                Dictionary.Remove(keyName);
            }

            public People()
            {
            }

            public Person this[string keyName]
            {
                get
                {
                    return (Person)Dictionary[keyName];
                }
                set
                {
                    Dictionary[keyName] = (Person)value;
                }
            }
        }

2.    ... Add to the Person class
        public static bool operator >(Person person1, Person person2)
        {
            return person1.Age > person2.Age;
        }

        public static bool operator <(Person person1, Person person2)
        {
            return person1.Age < person2.Age;
        }

        public static bool operator >=(Person person1, Person person2)
        {
            return person1.Age >= person2.Age;
        }

        public static bool operator <=(Person person1, Person person2)
        {
            return person1.Age <= person2.Age;
        }

3.    ... Add to the People class
        public Person[] GetOldest()
        {
            // default field to true for first comparison
            bool isOld = true;

            // Create an empty collection
            ArrayList crowd = new ArrayList();

            // Scan though the dictionary
            foreach (DictionaryEntry theSource in this)
            {
                if (crowd.Count > 0)
                    // Test if current person's age is at least equal to collection value
                    isOld = ((Person)theSource.Value >= (Person)crowd[0]);

                if (isOld)
                {
                    // Now test if this current age is older than current collection
                    if (crowd.Count > 0 && (Person)theSource.Value > (Person)crowd[0])
                    {
                        crowd.Clear();
                    }

                    // Add this person to collection
                    crowd.Add((Person)theSource.Value);
                }
            }

            Person[] result = new Person[crowd.Count];
            crowd.CopyTo(result, 0);

            return result;
        }

4.    ... Modify the People class declaration
        public class People : DictionaryBase, ICloneable

        ... Add this method to People class
        public object Clone()
        {
            Person twin;
            People deepCopy = new People();
            foreach (DictionaryEntry guy in this)
            {
                twin = (Person)((Person)guy.Value).Clone();
                deepCopy.AddPerson(twin);
            }

            return deepCopy;
        }

        ... Modify the Person class declaration
        public class Person : ICloneable

        ... Add this method to the Person class
        public object Clone()
        {
            // Only need a shallow copy of this class
            return MemberwiseClone();
        }

Go to topic 19745

Return to index page 765
Return to index page 764
Return to index page 763
Return to index page 762
Return to index page 761
Return to index page 760
Return to index page 759
Return to index page 758
Return to index page 757
Return to index page 756