Solved

public CompareField SortBy = CompareField.Name;

Posted on 2014-09-21
1
156 Views
Last Modified: 2014-09-22
Was wondering why do I need this line of code if someone can explain...

       public CompareField SortBy = CompareField.Name;

class CarComparer
    {
        public enum CompareField {
        Name,
        MaxMph,
        Horsepower,
        Price,
        
        }
        public CompareField SortBy = CompareField.Name;

        public int Compare(Cars x, Cars y) {

            switch (SortBy)
            {
                case CompareField.Name:
                    return x.Name.CompareTo(y.Name);
                case CompareField.MaxMph:
                    return x.MaxMph.CompareTo(y.MaxMph);
                case CompareField.Horsepower:
                    return x.Horsepower.CompareTo(y.Horsepower);
                case CompareField.Price:
                    return x.Price.CompareTo(y.Price);
            
            
            }

            return x.Name.CompareTo(y.Name);
        }
    }

Open in new window

0
Comment
Question by:yguyon28
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40335441
The line of code,

public CompareField SortBy = CompareField.Name;

is selecting the field to be used in the class CarComparer for comparison with two objects of type Cars. The problem is that the SortBy field is hard coded to compare on the property Name of the Cars object and therefore most of the code will never be executed and only allowing the comparison on the Name property and no more.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now