![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
|
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 |
#7
| |||
| |||
|
|
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 |
#8
| |||
| |||
|
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 |
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |