Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 12th, 2011, 03:05 PM
Authorized User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 27 Ex 2

In the solution for Ex 2, i dont see a call to the 'Compare' method in the CarComparer class...the method that has two car objects as parameters am i missing something?
  #2 (permalink)  
Old August 14th, 2011, 04:59 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I can see it in my version it looks like this:

Code:
// Compare two cars.
public int Compare(Car car1, Car car2)
{
    // Compare differently depending on the sort type.
    switch (Sort)
    {
        case SortType.ByName:
            return car1.Name.CompareTo(car2.Name);
        case SortType.ByMaxSpeed:
            return -car1.MaximumSpeed.CompareTo(car2.MaximumSpeed);
        case SortType.ByHorsepower:
            return -car1.Horsepower.CompareTo(car2.Horsepower);
        case SortType.ByPrice:
            return -car1.Price.CompareTo(car2.Price);
    }

    // This should never happen (unless we add a
    // new SortStyle value and forget to update the cases.)
    throw new Exception("Unknown SortType value");
}
Does your version of the program run? It shouldn't work if that method isn't there. Perhaps there was something wrong with the download?

If you can't get your version to work, email me at RodStephens@CSharpHelper.com and I'll send you a new version.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old August 14th, 2011, 06:01 PM
Authorized User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Rod,
Yes I do see the method. But i dont see a call to the method. The program runs fine but I'm just trying to figure out what I am missing...

Last edited by caa5042; August 14th, 2011 at 07:34 PM..
  #4 (permalink)  
Old August 14th, 2011, 10:24 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Ah. Gotcha.

The ListBox control itself calls the Compare method when it needs to sort the items. If you set a breakpoint in the Compare method, you'll see the code stop several times when the control needs to sort the items.

(The ListBox knows that the object has a Compare method because it implements IComparer.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old August 14th, 2011, 11:45 PM
Authorized User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jul 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

oh...so we dont have to invoke the method for it to actually execute its code?...i see the Compare method has two car objects as parameters....How does the program know to call that method and pass two car objects to it if there is no code in there that does that?...
  #6 (permalink)  
Old August 15th, 2011, 08:56 AM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Sometimes you don't need to invoke the method yourself, particularly when you implement an interface. Often you are required to provide an object that implements an interface just so the thing using it knows that it provides a certain service, in this case comparing two objects.

An array's Sort method can take as a parameter an object that implements IComparer. that lets the Sort method know that this object can sort.

It's a generic interface so the Sort method doesn't know what type of things it can compare (in this case it's Cars), but it knows that the array contains whatever those things are. So it blindly uses the IComparer to compare the objects in the array without knowing what they are. It doesn't need to know, it just needs to know how to compare them.

You could even do that yourself. If I gave you a list of objects and a way to tell which was bigger for any two of them, you could run through the list comparing them to find the "biggest."

To summarize, usually you write code for your code to invoke, sometimes your code calls code provided by the system, and sometimes you write code that the system can call for you.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 


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
Chapter 27-3 column Mark P. BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 7 July 1st, 2011 05:04 PM
Chapter 27 Java Novice BOOK: Java Programming 24-Hour Trainer by Yakov Fain 0 May 15th, 2011 08:26 AM
HTTPModule Chapter 27 sherbug BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 4 August 21st, 2010 02:03 PM
Chapter 27: Modules And Handlers - Listing 27.2 Nikhil BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 February 4th, 2010 07:25 AM



All times are GMT -4. The time now is 04:09 AM.


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