Click here to Skip to main content
15,906,081 members
Articles / Programming Languages / C# 4.0
Alternative
Tip/Trick

Ad-Hoc Expression Evaluation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Jul 2012CPOL 10.3K   2   2
This is an alternative for "Ad-Hoc Expression Evaluation"

Using the Code

JSOP's initial tip was to perform the following:

  • Compare two objects of the same type
  • Use a string to determine what sort of comparison would take place

The initial solution converted each object into a known type (string, decimal, or DateTime) prior to performing a comparison. Using the CompareTo<T> interface, the code JSOP provided could be significantly simplified. The following is the JSOP's Evaluator rewritten to use generics:

C#
public static class GenericEvaluator
{
    public static bool Evaluate<T>(string comparison, T obj1, T obj2) where T : IComparable<T>
    {
        bool result = false;
 
        // Just to make our comparisons easier.
        comparison = comparison.ToUpper();
 
        if ((comparison == "AND") || (comparison == "OR"))
        {
            if (obj1 is bool)
            {
                if (comparison == "AND")
                    result = ((bool)(object)obj1 && (bool)(object)obj2);
                else
                    result = ((bool)(object)obj1 || (bool)(object)obj2);
            }
        }
        else
        {
            int compareTo = obj1.CompareTo(obj2);
 
            switch (comparison)
            {
                case "EQUAL":                 result = (compareTo == 0); break;
                case "NOT EQUAL":             result = (compareTo != 0); break;
                case "GREATER THAN":          result = (compareTo >  0); break;
                case "LESS THAN":             result = (compareTo <  0); break;
                case "GREATER THAN OR EQUAL": result = (compareTo >= 0); break;
                case "LESS THAN OR EQUAL":    result = (compareTo <= 0); break;
            }
        }
 
        return result;
    }
} 

Further improvements could be made by using something other than a string to determine which comparison is made, but I decided to keep with the original requirements JSOP provided.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
QuestionNew requirement Pin
#realJSOP26-Jul-12 3:48
professional#realJSOP26-Jul-12 3:48 
AnswerRe: New requirement Pin
Andrew Rissing26-Jul-12 4:47
Andrew Rissing26-Jul-12 4:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.