Solved

public CompareField SortBy = CompareField.Name;

Posted on 2014-09-21
1
165 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

627 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