Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 5th, 2004, 06:37 PM
Authorized User
 
Join Date: Jul 2004
Location: Saint Peters, Missouri, USA.
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();
        }
Reply With Quote
  #2 (permalink)  
Old August 9th, 2004, 10:48 AM
Authorized User
 
Join Date: Jul 2004
Location: Saint Peters, Missouri, USA.
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;
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:01 PM.


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