Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 November 19th, 2011, 01:25 PM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default Chapter 11 Ch10Ex05

namespace Ch11Ex05
{
class Person: IComparable
{
public string Name;
public int Age;

public Person(string name, int age)
{
Name = name;
Age = age;
}

public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age + otherPerson.Age;
}
else
{
throw new ArgumentException(
"Object to compare to is not a Person object.");
}
}
}
}

WHAT MEANS

return this.Age - otherPerson.Age;
i replaced - with +

and results are:
people sorted with default comparer by age:
Bert 27
Jim 30
Ernie 22
Bob 25

Peoples are not sorted by age

Initials data:

ArrayList list = new ArrayList();
list.Add(new Person("Jim", 30));
list.Add(new Person("Bob", 25));
list.Add(new Person("Bert", 27));
list.Add(new Person("Ernie", 22));
 
Old November 19th, 2011, 03:37 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

The return value of CompareTo should be 0 if both objects have the same order value, or a minus or a positive value depending if the first or the second object should go first. An easy way to compare this is by invoking the CompareTo method of the Int32 type.
Code:
public int CompareTo(object obj)
{
if (obj is Person)
{
Person otherPerson = obj as Person;
return this.Age.CompareTo(therPerson.Age);
}
else
{
throw new ArgumentException(
"Object to compare to is not a Person object.");
}
}
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel

Last edited by ChristianNagel; November 19th, 2011 at 03:42 PM.. Reason: was incomplete
The Following User Says Thank You to ChristianNagel For This Useful Post:
dampyr (November 20th, 2011)
 
Old November 20th, 2011, 06:35 AM
Authorized User
 
Join Date: Nov 2011
Posts: 34
Thanks: 14
Thanked 0 Times in 0 Posts
Default

Thanks again,i have one question:I want to dive into database programming (MS SQL ) using c#,that's why i bought this book,to learn basic c#.Which book(s) would you recommend for developing database applications using c#.I have solid knowledge of SQL language/?

Last edited by dampyr; November 20th, 2011 at 06:39 AM.. Reason: corrections





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 11 Help please Soccerguy BOOK: Beginning ASP.NET 4 : in C# and VB 2 June 10th, 2011 06:04 AM
Chapter 11 icculus1 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 July 30th, 2009 05:15 PM
chapter 11 figure 11-7 relative positioning pelopito BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 2 November 29th, 2007 06:11 AM
Chapter 11: kiley-s BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 10 March 8th, 2007 08:19 PM
Chapter 11 kappa3 Wrox Book Feedback 0 October 8th, 2003 11:21 AM





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