Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the 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 August 5th, 2004, 06:37 PM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning Visual C# Exercises - Chapter 11

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();
        }
 
Old August 9th, 2004, 10:48 AM
Authorized User
 
Join Date: Jul 2004
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

3. Alternative overloads to the last two per official exercise answers
        public static bool operator >=(Person person1, Person person2)
        {
            return !(person1 < person2);
        }

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

3.Could have converted employing "AS" operator (see next example).

4.Alternative conversion employing "AS" operator
        Person original;
        People deepCopy = new People();
        foreach (DictionaryEntry guy in this)
        {
            original = guy.Value as Person;
            deepCopy.AddPerson((Person)original.Clone());
        }

        return deepCopy;





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginning Visual C# Exercises - Chapter 05 seblake C# 1 July 26th, 2004 07:40 AM
Beginning Visual C# Exercises - Chapter 04 seblake C# 0 July 21st, 2004 09:21 AM
Beginning Visual C# Exercises - Chapter 07 seblake C# 0 July 20th, 2004 02:07 PM
Beginning Visual C# Exercises - Chapter 06 seblake C# 1 July 19th, 2004 09:15 AM





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