HighTechTalks DotNet Forums  

Evaluating a string as a IF condition

Dotnet Academic General Discussions microsoft.public.dotnet.academic


Discuss Evaluating a string as a IF condition in the Dotnet Academic General Discussions forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Pedro
 
Posts: n/a

Default Evaluating a string as a IF condition - 03-29-2007 , 01:54 PM






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

Reply With Quote
  #2  
Old   
pvdg42
 
Posts: n/a

Default Re: Evaluating a string as a IF condition - 03-30-2007 , 07:42 AM







"Pedro" <Pedro (AT) discussions (DOT) microsoft.com> wrote

Quote:
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
No. The expression following if must be a boolean, and a string does not
satisfy that requirement.




Reply With Quote
  #3  
Old   
Wibberlet
 
Posts: n/a

Default RE: Evaluating a string as a IF condition - 03-30-2007 , 09:22 AM



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

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

"Pedro" wrote:

Quote:
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

Reply With Quote
  #4  
Old   
Pedro
 
Posts: n/a

Default RE: Evaluating a string as a IF condition - 03-31-2007 , 07:36 AM



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

"Wibberlet" wrote:

Quote:
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

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

"Pedro" wrote:

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

Reply With Quote
  #5  
Old   
Wibberlet
 
Posts: n/a

Default RE: Evaluating a string as a IF condition - 03-31-2007 , 08:14 AM



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

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

"Pedro" wrote:

Quote:
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

"Wibberlet" wrote:

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

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

"Pedro" wrote:

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

Reply With Quote
  #6  
Old   
Pedro
 
Posts: n/a

Default RE: Evaluating a string as a IF condition - 04-01-2007 , 06:18 AM



Thanks again.

I will see what workaround I can do...

Pedro

"Wibberlet" wrote:

Quote:
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

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

"Pedro" wrote:

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

"Wibberlet" wrote:

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

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

"Pedro" wrote:

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

Reply With Quote
  #7  
Old   
Pedro
 
Posts: n/a

Default RE: Evaluating a string as a IF condition - 04-01-2007 , 07:38 AM



Code Project rules

http://www.codeproject.com/csharp/rp...sionparser.asp

Pedro

"Wibberlet" wrote:

Quote:
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

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

"Pedro" wrote:

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

"Wibberlet" wrote:

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

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

"Pedro" wrote:

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

Reply With Quote
  #8  
Old   
Loy
 
Posts: n/a

Default Re: Evaluating a string as a IF condition - 04-10-2007 , 12:12 PM



On Apr 1, 2:38 pm, Pedro <P... (AT) discussions (DOT) microsoft.com> wrote:
Quote:
Code Project rules

http://www.codeproject.com/csharp/rp...sionparser.asp

Pedro

"Wibberlet" wrote:
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 athttp://wibberlet.blogspot.com

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

"Pedro" wrote:

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

"Wibberlet" wrote:

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 athttp://wibberlet.blogspot.com

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

"Pedro" wrote:

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
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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.