Solved

public CompareField SortBy = CompareField.Name;

Posted on 2014-09-21
1
163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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