Click to See Complete Forum and Search --> : Tricky Problem
viper161616
July 20th, 2008, 08:27 AM
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:
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!
JonnyPoet
July 20th, 2008, 02:24 PM
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
"((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 ?
dglienna
July 20th, 2008, 03:37 PM
VB6 had the EVAL() function that worked like this (and could be very dangerous to let users use)
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
viper161616
July 20th, 2008, 06:06 PM
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 ?
dglienna
July 20th, 2008, 06:26 PM
try JSCRIPT
JonnyPoet
July 20th, 2008, 06:47 PM
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.
viper161616
July 20th, 2008, 09:03 PM
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.
JonnyPoet
July 21st, 2008, 06:08 AM
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
List<ICompareable> myCompareClassesList = new List<ICompareable>();
viper161616
July 22nd, 2008, 01:23 AM
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?
cjard
July 22nd, 2008, 07:40 AM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.