Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2007

Tip: Looking for answers? Try searching our database.

Evaluating a string as a IF condition

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Pedro - 29 Mar 2007 18:54 GMT
Hello,

I was woundering if it's possible to use the content of a string as the
condition on a IF statement.

Example:

string c = "(nb == 10)";

int nb = 9;

if( c )
{
// something if true
}
else
{
// something if false
}

Thanks in advance

Pedro
pvdg42 - 30 Mar 2007 12:42 GMT
> Hello,
>
[quoted text clipped - 19 lines]
>
> Pedro

No. The expression following if must be a boolean, and a string does not
satisfy that requirement.
Wibberlet - 30 Mar 2007 14:22 GMT
Hi Pedro,

An If statement must always evaluate to either true or false; in other
words, what follows the If statement must  be a boolean expression.

examples are:

bool booleanFlag = true;
bool booleanFlag = (nb == 10);

if (booleanFlag)
{
   //Conditional logic here
}

Were you trying to solve a particular problem or is your query just a
general question? I would be happy to try and help if you have any further
questions.

wibbleret
Development blog at http://wibberlet.blogspot.com

===============================================

> Hello,
>
[quoted text clipped - 19 lines]
>
> Pedro
Pedro - 31 Mar 2007 12:36 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.