Thank you both,
I know that an IF must evaluate a boolean, I wanted to know if it's possible
evaluate a string content as a boolean.
What I wanted to do was to store in a database a string that allows me to
modify a IF statment at runtime.
In the example I gave I have a variable in the code "nb" that I have to test
to do something if it's true or another if it's false, I wanted to store that
IF condition on a database because I want to be able to chance that condition
without compiling the code.
Example: today the test is (nb == 10) and tomorow I would like to be (nb ==
20)
As so I was woundring if it's possible to evaluate the content of a string
"nb == 10" to return a true or false.
Pedro
> Hi Pedro,
>
[quoted text clipped - 43 lines]
> >
> > Pedro
Wibberlet - 31 Mar 2007 13:14 GMT
Hi Pedro,
Now I understand what you are trying to do. Essentially what you are after
is an Eval() statement that exists in many scripting languages (it's much
easier when things are interpreted) but not in C#.
In VBScript you could use the Select case true statement as shown below:
Select case true
case a = 3
Conditional code here
case b = 6.2
Conditional code here
case c = "MyStringvalue"
Conditional code here
End select
There is no similar construct in C#
I assume that in your example, the variable name also changes since
otherwise you could just parameterise the value itself?
e.g.
if (nb == parameter1) etc
So basically I think you have 2 options.
1. Restructure the code to enable a viable C# construct.
2. Write an expression parser - altho this is a lot harder than it may seem
Any other possible options depend on how many conditions you are evaluating
- or how many there could be.
If you need any more info please describe the problem in more detail and I
will try my best to help you.
regards - matt
Development blog at http://wibberlet.blogspot.com
===============================================
> Thank you both,
>
[quoted text clipped - 62 lines]
> > >
> > > Pedro
Pedro - 01 Apr 2007 11:18 GMT
Thanks again.
I will see what workaround I can do...
Pedro
> Hi Pedro,
>
[quoted text clipped - 104 lines]
> > > >
> > > > Pedro
Pedro - 01 Apr 2007 12:38 GMT
Code Project rules :)
www.codeproject.com/csharp/rpn_expressionparser.asp
Pedro
> Hi Pedro,
>
[quoted text clipped - 104 lines]
> > > >
> > > > Pedro
Loy - 10 Apr 2007 17:12 GMT
> Code Project rules :)
>
[quoted text clipped - 110 lines]
>
> > > > > Pedro
Your tool is called ScriptControl (as mentioned before)
There is a com object like that which I used with c# quite well
The statements (stings) though are either vbscript or jscript syntax
The objects you use can freely be dotnet (c#) objects
Using the script control you can, not only evaluate, but also invoke
statements specified as DB strings
HTH
Let me know if you need me to look for that old code of mine, or you
can manage from here
Loy