-2

Let's say I have 3 strings

string a = "Dog";
string b = "==";
string c = "cat";

How could I use them in an If Statement as follows

if (a b c)
{

     return true;

}else{

     return false;

}

The idea behind this low code, or a visual coding platform where a user enters the 3 portions

Condition 1
The Operator
Condition 2

And returns true or false

  • You need to [explain what you are trying to accomplish](https://meta.stackexchange.com/questions/66377/) because what you are doing is not how C# works. – Dour High Arch Mar 15 '20 at 17:38
  • 1
    I think you are looking for something like this: https://github.com/filipw/csharp-string-to-lambda-example – Oguz Ozgul Mar 15 '20 at 17:43
  • This explains all you need to know [Convert string value to operator in C#](https://stackoverflow.com/questions/7086058/convert-string-value-to-operator-in-c-sharp) – Barns Mar 15 '20 at 17:52

2 Answers2

2

You can write a possible operator in if-else or switch operator, and then you can compare with your user input, and according to that, you can perform your operation.

For Example:-

If (b == "==")
{
 return a==c;
}
0

In simple words: You want to create an expression (in specific, a bool-result-expression)

Fast way:

string a;
string b;
string c;

bool? result = null; //I prefer nullable in case of absence of match
if(b == "=="){
    result = Equals(a, c); //This is for equality, you can create your own methods and check what you want
}

Correct way: use an expression parser. There are a lot of different options in the community, well tested and continued.

DrkDeveloper
  • 939
  • 7
  • 17