Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating logical string

Creating logical string

Scheduled Pinned Locked Moved C#
helpquestiondata-structurestestingbeta-testing
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guinness4Strength
    wrote on last edited by
    #1

    I'm not sure how to solve this problem and I'm hoping someone can help. I'm writing an application that allows the user to configure logical operations using IF,THEN,AND,OR operators, but I'm a bit confused as to the best way to evaluate the conditional statement once it is built. For examples lets say that the user specifies a three part conditional statement and I have evaluated each part and stored the correct boolean value in an ArrayList. How do I evaluate all the parts of the condition at once ?

    ArrayList Parts = new ArrayList();
    //Add values for testing
    Parts.Add(false);
    Parts.Add(false);
    Parts.Add(true);
    bool Result=false;
    //this assumes the OR operator is used false || false || true = true
    for(int x=0;x

    If I evaluate the array in groups of two, the condition is incorrectly false when comparing the first two values (false || false).

    What's the best way to evaluate user defined conditional statements ?
    Remeber there can be X number of conditionals and both the || and && logical operators can be used.
    Thanks for taking the time to read this lengthy message....

    C G 2 Replies Last reply
    0
    • G Guinness4Strength

      I'm not sure how to solve this problem and I'm hoping someone can help. I'm writing an application that allows the user to configure logical operations using IF,THEN,AND,OR operators, but I'm a bit confused as to the best way to evaluate the conditional statement once it is built. For examples lets say that the user specifies a three part conditional statement and I have evaluated each part and stored the correct boolean value in an ArrayList. How do I evaluate all the parts of the condition at once ?

      ArrayList Parts = new ArrayList();
      //Add values for testing
      Parts.Add(false);
      Parts.Add(false);
      Parts.Add(true);
      bool Result=false;
      //this assumes the OR operator is used false || false || true = true
      for(int x=0;x

      If I evaluate the array in groups of two, the condition is incorrectly false when comparing the first two values (false || false).

      What's the best way to evaluate user defined conditional statements ?
      Remeber there can be X number of conditionals and both the || and && logical operators can be used.
      Thanks for taking the time to read this lengthy message....

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Okay - so you've parsed the user input and you have a bunch of expressions which evaluate to a boolean and the logical operators. What I've done in the past is create a class that represents a filter. A filter can be any sequence of conditional statements. The Filter class has three derived classes. AndFilter, OrFilter and BooleanFilter. The base class Filter looks like this:

      public abstract class Filter
      {
      public abstract bool Evalutate();
      }

      The concrete classes override Evaluate are BooleanFilter - this is a simple one. Its job is just to return the boolean value it was created with. This is what you create for each conditional statement (I'll show you how to plug it all together in a moment):

      public class BooleanFilter : Filter
      {
      private bool value;
      public BooleanFilter(bool value)
      {
      this.value = value;
      }

      public override bool Evaluate()
      {
          return value;
      }
      

      }

      Then there is the AndFilter (the OrFilter class is almost identical so I'll not show it). The job of this class is to determine if the Evaluation of Filter A && Filter B

      public class AndFilter : Filter
      {
      private Filter filterA;
      private Filter filterB;

      public AndFilter(Filter filterA, Filter filterB)
      {
          this.filterA = filterA;
          this.filterB = filterB;
      }
      
      public override bool Evaluate()
      {
          // Remember in the OrFilter version to change the & to an |
          bool result = filterA.Evaluate() & filterB.Evaluate();
          return result;
      }
      

      }

      Okay - So we have our filter classes now. All we need to do is to chain them up. Now, you have probably realised we are still evaluating in groups of two, but where this differs from your design is that this will evaluate a tree structure, not a sequential structure. Always you are comparing the nodes on the left to the nodes on the right. Some example code:

      BooleanFilter a = new BooleanFilter(false);
      BooleanFilter b = new BooleanFilter(false);
      BooleanFilter c = new BooleanFilter(true);
      BooleanFilter d = new BooleanFilter(true);

      AndFilter andAB = new AndFilter(a, b); // andAB.Evaluate() == false;
      AndFilter andBC = new AndFilter(b, c); // andBC.Evaluate() == false;
      OrFilter orBC = new OrFilter(b, c); // orBC.Evaluate() == true;

      // a OR b OR c - Combines the result of previous OrFilter with a BooleanFilter
      Filter aOrBOrC = OrFilter(a, orBC);

      // Watch the way the query is being built up though:
      // a AND b OR c == (a AND (

      1 Reply Last reply
      0
      • G Guinness4Strength

        I'm not sure how to solve this problem and I'm hoping someone can help. I'm writing an application that allows the user to configure logical operations using IF,THEN,AND,OR operators, but I'm a bit confused as to the best way to evaluate the conditional statement once it is built. For examples lets say that the user specifies a three part conditional statement and I have evaluated each part and stored the correct boolean value in an ArrayList. How do I evaluate all the parts of the condition at once ?

        ArrayList Parts = new ArrayList();
        //Add values for testing
        Parts.Add(false);
        Parts.Add(false);
        Parts.Add(true);
        bool Result=false;
        //this assumes the OR operator is used false || false || true = true
        for(int x=0;x

        If I evaluate the array in groups of two, the condition is incorrectly false when comparing the first two values (false || false).

        What's the best way to evaluate user defined conditional statements ?
        Remeber there can be X number of conditionals and both the || and && logical operators can be used.
        Thanks for taking the time to read this lengthy message....

        G Offline
        G Offline
        Guinness4Strength
        wrote on last edited by
        #3

        I figured it out. I actually had the right solution the whole time, I just needed to let the loop run its course instead of using the break command when it equated to false. Dumb....very dumb.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups