Solved

public CompareField SortBy = CompareField.Name;

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

929 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

12 Experts available now in Live!

Get 1:1 Help Now