Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

public CompareField SortBy = CompareField.Name;

Posted on 2014-09-21
1
Medium Priority
?
166 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 64

Accepted Solution

by:
Fernando Soto earned 1500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

670 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