 |
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
|
|
|
|

February 6th, 2004, 04:45 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
chapter11
Who have the answer of chapter11 q1.
thank U very much.
|
|

June 12th, 2004, 09:26 PM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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);
}
}
}
|
|

June 12th, 2004, 10:41 PM
|
|
Registered User
|
|
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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);
|
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 12:06 PM |
| 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 |
|
 |