CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Tricky Problem

  1. #1
    Join Date
    Jul 2008
    Posts
    7

    Tricky Problem

    Hey, I have looked but couldn't find an answer to my question within the forum. Furthermore, I believe this is the correct place to ask as I am looking for a c# specific answer. If one exist please redirect me, otherwise:
    I was wondering if it is at all possible to pass a segment a of code/argument to an if statement so that it can therein be evaluated. Obviously, this example won't work and I know I could just evaluate the string and make a decision from there, but I was wondering if there was any shortcut or method of passing the entire argument to be evaluated to a function that anyone knew of.
    Here is essentially what I want to do:
    Code:
    public void OnButton1Clicked (object sender, System.EventArgs e)
    	{
    		tryStuff("x < y".);
                    tryStuff("x> y".);
                    tryStuff("x>y && x > z")
    	}
    	public void tryStuff(string s)
    	{
    		int x = 5;
    		int y = 10;
                    int z = 3;
    		if (s)
    		{
    			//do whatever
    		}
    	}
    Thanks in advance to anyone who helps!
    Last edited by cjard; July 22nd, 2008 at 07:38 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Tricky Problem

    Hi !

    At first please use codetags as this is standard in the forum to get posts answered.
    At second: The answer could be easy if you only need the above conditions but it will get complictaed if you want to really parse any string and then executing the given string independent of what is inside. Maybe it should also work when the string is like
    Code:
    "((a+b)> c)? (3*c+ 5b^2) : (0.5*c / b-a)"
    This would need a complcated parsing system as used in compilers. So whats your real needs ? Expressions known in before or any expressions to be handled ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Tricky Problem

    VB6 had the EVAL() function that worked like this (and could be very dangerous to let users use)
    Code:
    Option Explicit
    
    ' Add a reference to Microsoft Script Control 1.0
    
    Private Sub Form_Load()
    Dim str1 As String
    Dim dbl1 As Double
    Dim x As New ScriptControl
    Dim z%, a%, q$
    
    z = 55
    a = 24
    q = "*"
    x.Language = "vbScript"
    MsgBox Format(x.Eval(z & q & a), "standard")
     
    ' ===============================================
    
     str1 = "(5+50)*2/3"
     dbl1 = (x.Eval(str1))
     MsgBox dbl1
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Jul 2008
    Posts
    7

    Re: Tricky Problem

    Thanks for your help so far. My bad about the code tags.
    Basically I want to be able to load up an array list with a bunch of simple statements that you might typically find in an if statement so that I can loop through them and evaluate them as a bool. I just need fairly simple commands such as >, <, |, &, +, - ,/, *. I don't believe their is an equivalent to the eval function in c#. It's not essential it would just make things simpler if I could just arraylist.add(a bunch of things) then loop through the arraylist and influence a parallel arraylist based on how the first evaluated.
    If this isn't possible is there anyway to store just an operator in an arraylist/array ?
    Last edited by viper161616; July 20th, 2008 at 06:16 PM.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Tricky Problem

    try JSCRIPT
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Tricky Problem

    Quote Originally Posted by viper161616
    Thanks for your help so far. My bad about the code tags.
    Basically I want to be able to load up an array list with a bunch of simple statements that you might typically find in an if statement so that I can loop through them and evaluate them as a bool. I just need fairly simple commands such as >, <, |, &, +, - ,/, *. I don't believe their is an equivalent to the eval function in c#. It's not essential it would just make things simpler if I could just arraylist.add(a bunch of things) then loop through the arraylist and influence a parallel arraylist based on how the first evaluated.
    If this isn't possible is there anyway to store just an operator in an arraylist/array ?
    Maybe you might have a bunch of different math signs as a string and then a switch statement which does different calculations depending on the string. Maybe an Interface is a solution for you where the interface then defines the math calcultion like in a strategy pattern.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  7. #7
    Join Date
    Jul 2008
    Posts
    7

    Re: Tricky Problem

    I'm not exactly sure what you mean but I guess I am probably just going to parse the string or use regex expressions to get what I'm looking for done. Is there anyway that if say I have a string with the name of a variable that I could somehow get that variable? For example say int x = 3, string y = "x") is there a method that I could use y to get the data in the variable x? I'm not sure if I made that clear enough so if not I can specify more.

  8. #8
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Tricky Problem

    Quote Originally Posted by viper161616
    Thanks for your help so far. My bad about the code tags.
    Basically I want to be able to load up an array list with a bunch of simple statements that you might typically find in an if statement so that I can loop through them and evaluate them as a bool. I just need fairly simple commands such as >, <, |, &, +, - ,/, *. I don't believe their is an equivalent to the eval function in c#. It's not essential it would just make things simpler if I could just arraylist.add(a bunch of things) then loop through the arraylist and influence a parallel arraylist based on how the first evaluated.
    If this isn't possible is there anyway to store just an operator in an arraylist/array ?
    Basically what I can understand is you have a lot of different comparing orders which needs to be evaluated. why you want to have thema as strings ?
    So maybe use the ICompareable interface, add it to whatever classes you want to compare then built different sorts of comparing algos and for each one have your Comparer class. each Comparer has its own defined work to do but basically they all compare in a way identic, bigger as, smaller as, ( +1, 0 ,-1 ) as its output. The list where you store all your Comparer classes is of Type
    Code:
    List<ICompareable> myCompareClassesList = new List<ICompareable>();
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  9. #9
    Join Date
    Jul 2008
    Posts
    7

    Re: Tricky Problem

    Well thanks for all your help so far. I have one last question. I know that if I am using a treeview then I can assign a filter (Gtk.TreeModelFilterVisibleFunc (FilterTree), where filter tree is the function that determines if an item shows up or not.) to the model. If I wanted to assign a filter function to my own class how might I do this, or where is a tutorial on this? Is it even possible?

  10. #10
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Tricky Problem

    i'd tend to agree with the others in that running such things as a script (they need to be parsed and compiled on their own to become anything other than just a string) would save most work..

    by writing a parser etc youre merely repeating work microsoft have already done
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured