p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2004, 04:45 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2004
Location: TianJin, , China.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Doll Send a message via MSN to Doll Send a message via Yahoo to Doll
Default chapter11

Who have the answer of chapter11 q1.
thank U very much.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old June 12th, 2004, 10:26 PM
Registered User
Points: 4, Level: 1
Points: 4, Level: 1 Points: 4, Level: 1 Points: 4, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm a relative newbie when it comes to C# but here's what I came up with for the answer to the first question in the Exercise section of Chapter 11. I've included the code to two .CS files, one that creates the collection and one that uses it. Hope this helps. Let me know if anyone has any issues with this or has a better idea for handling the question.

Here's the collection stuff -

using System;
using System.Collections;

namespace Ch11People
{
    public class Person
    {
        private string name;
        private int age;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                age = value;
            }
        }

        // This is the constructor for the Person class

        public Person(string newName, int newAge)
        {
            name = newName;
            age = newAge;
        }
    }

    // This defines the Collection of persons, namely People.

    public class People : CollectionBase
    {
        public void Add(Person newPerson)
        {
            List.Add(newPerson);
        }


        // This is the 'normal' indexer, using an integer to get at the collection.

        public Person this[int person_index]
        {
            get
            {
                return (Person)List[person_index];
            }
        }

        // This is the string indexer.

        public Person this[ string name ]
        {
            get
            {
                int i;

                for (i = 0;i < List.Count;i++)
                {
                    Person tmp_person = (Person) List[i];

                    if ( tmp_person.Name == name )
                    {
                        i = List.Count;
                    }
                }

                if (i == List.Count)
                    return (Person)List[i];
                else
                    return(new Person("John Doe", 999));
            }
        }
    }
}


and here's the client that uses it.

using System;
using Ch11People;

namespace PeopleClient
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // Add some people to the collection.

            People personCollection = new People();
            personCollection.Add(new Person("Harold", 21));
            personCollection.Add(new Person("James", 35));
            personCollection.Add(new Person("William", 73));
            personCollection.Add(new Person("Harold", 75));

            // Display each person in the collection.

            foreach (Person person in personCollection)
            {
                Console.WriteLine("This is {0}, who is {1} years old.", person.Name, person.Age);
            }

            // Display using the integer indexer.

            Console.WriteLine(personCollection[0].Name);
            Console.WriteLine(personCollection[1].Name);
            Console.WriteLine(personCollection[2].Name);

            // Look for a person using the string indexer.

            int count = 0;
            for (int i = 0; i < personCollection.Count;i++)
            {
                if (personCollection[i].Name == "Harold")
                    count += 1;
            }

            Console.WriteLine("I found {0} person(s) named Harold.", count);
        }
    }
}


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old June 12th, 2004, 11:41 PM
Registered User
Points: 4, Level: 1
Points: 4, Level: 1 Points: 4, Level: 1 Points: 4, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I realized that my code is not quite what it should be. I thought I was using the string indexer when I wasn't. Please replace in the collection code -

public Person this[ string name ]
        {
            get
            {
                int i;

                for (i = 0;i < List.Count;i++)
                {
                    Person tmp_person = (Person) List[i];

                    if ( tmp_person.Name == name )
                    {
                        i = List.Count;
                    }
                }

                if (i == List.Count)
                    return (Person)List[i];
                else
                    return(new Person("John Doe", 999));
            }
        }

with -

public Person this[ string name ]
        {
            get
            {
                int i;
                int j = 0;

                for (i = 0;i < List.Count;i++)
                {
                    Person tmp_person = (Person) List[i];

                    if ( tmp_person.Name == name )
                    {
                        j = i;
                        // I have to subtract one because the 'for' loop increments i and then checks to
                        // see if we reached the end of the loop. As a result, i will be one greater than
                        // it should be if I don't subtract one. A bit kludgy, I know.
                        i = List.Count -1;
                    }
                }

                if (i == List.Count)
                    return (Person)List[j];
                else
                    return(new Person("John Doe", 999));
            }
        }

In addition, in the client, replace this part -

// Look for a person using the string indexer.

            int count = 0;
            for (int i = 0; i < personCollection.Count;i++)
            {
                if (personCollection[i].Name == "Harold")
                    count += 1;
            }

            Console.WriteLine("I found {0} person(s) named Harold.", count);

with -

// Look for a person using the string indexer.
            string searchName = "Harold";

            Person tmp_person = personCollection[searchName];

            if (tmp_person.Name == "John Doe")
                Console.WriteLine("No person with the name {0} was found.", searchName);
            else
                Console.WriteLine("We found at least one person who is name {0}", searchName);

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter11-ProposalManager.zip Password Protected ? ian BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 2 August 27th, 2008 01:06 PM
Chapter11 Anybody Get This Far????? MisterH BOOK: Wrox's Visual Basic 2005 Express Edition Starter Kit ISBN: 978-0-7645-9573-8 0 April 11th, 2008 07:36 AM
Chapter11 page 400 asplundo BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 February 18th, 2008 09:29 AM
error in chapter11 postcard.php GOUR CHANDRA PAUL BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 December 7th, 2005 07:30 PM
chapter11 login webshark BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 16 March 23rd, 2004 06:42 PM



All times are GMT -4. The time now is 08:22 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc