在IF语句中使用数组中的运算符

时间:2015-06-25 16:07:19

标签: c#

我正在尝试使用从if语句中的数组中选择的运算符。由于代码在下面,我无法编译它。反正有吗?

string[] operators = new string[]{"<",">","=","!="};

decimal value = Convert.ToDecimal(values[j]);
var operator1 = (operators[Convert.ToInt32(IQueryTypeList[k])]);
int jjj = Convert.ToInt32(NTestValueList[k]);

if (value operator1 jjj)
{
    IsActive = true;
}
else
{
    IsActive = false;
}

3 个答案:

答案 0 :(得分:2)

一种方法是使用if-else链或switch语句执行字符串比较。我不知道C#的确切语法,因此请将以下内容视为伪代码:

// Dependencies
var mysql = require("mysql"),
    http  = require("http");

// This holds our query results
var results;

// Connect to database
var connection = mysql.createConnection({
    host: "localhost",
    user: "root"
});
connection.connect(function(err) {
    if (err) throw err;
    console.log("Connected to database");
});
connection.query("SELECT * FROM myTable", function(err, rows, fields) {
    if (err) throw err;
    results = rows;
    connection.end(); // Disconnect from database
});

// Function to handle browser's requests
function requestHandler(req, res) {
   res.end(JSON.stringify(results)); // Respond to request with a string
}

// Create a server
var server = http.createServer(requestHandler);
// That magic number 8080 over here is the port our server listens to.
// You can access this webpage by visiting address http://localhost:8080
server.listen(8080, function() { 
    console.log("Server online");
});

答案 1 :(得分:0)

您不能简单地将字符串用作运算符,但您可以编写使用字符串的扩展方法。请查看here以获得更深入的解释。

答案 2 :(得分:0)

您可以使用开关案例逻辑:

string[] operators = new string[]{"<",">","=","!="};
decimal value = Convert.ToDecimal(values[j]);
var operator1 = (operators[Convert.ToInt32(IQueryTypeList[k])]);
int jjj = Convert.ToInt32(NTestValueList[k]);

switch (operator1)
{
    case "<":
        //do something here
        break;
    case ">":
         //do something here
        break;
    case "=":
        //do something here
        break;
    case "!=":
        //do something here
        break;
}