Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > BOOK: Beginning Visual C#
|
BOOK: Beginning Visual C#
This is the forum to discuss the Wrox book Beginning Visual C#, Revised Edition of Beginning C# for .NET v1.0 by Karli Watson, David Espinosa, Zach Greenvoss, Jacob Hammer Pedersen, Christian Nagel, Jon D. Reid, Matthew Reynolds, Morgan Skinner, Eric White; ISBN: 9780764543821
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2004, 04:15 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 11 Answers

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();
        }









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.