在C#中调用IF语句方法

时间:2017-01-29 21:53:23

标签: c# json visual-studio if-statement methods

我注意到我正在重复我的很多代码。我想要做的是将下面的if语句存储在方法中,以便可以随时调用它。我的问题是,我稍微了解方法,但是我很难为我想要的方法创建方法。下面是我的代码示例。注释掉的代码是我想要放在方法中的代码。非常感谢任何帮助。

if (result.Equals("") && type == "")
{
    JObject jobj = JObject.Parse(Helper.callerClass(@""));
    var titleTokens = jobj.SelectTokens("...title");
    var values = titleTokens.Select(x => (x as JProperty).Value);

    /*if (titleTokens.Contains(s))
    {
        MessageBox.Show("");
    }
    else
    {
        MessageBox.Show("");
    }*/
}

2 个答案:

答案 0 :(得分:0)

private bool Check(string result, string type) {
    if (result.Equals("") && type == "")
        {
            JObject jobj = JObject.Parse(Helper.callerClass(@""));
            var titleTokens = jobj.SelectTokens("...title");
            var values = titleTokens.Select(x => (x as JProperty).Value);

            return titleTokens.Contains(s);
        }
    return false;
}

你的意思是这样吗?

答案 1 :(得分:0)

//To call the function use the following line and replace yourClassName with you class name
  yourClassName myInstObj= new yourClassName();
  myInstObj.Check(result,type);

//Copied from the above answer b/c it should work fine.

private bool Check(string result, string type) {
   if (result.Equals("") && type == "")
    {
        JObject jobj = JObject.Parse(Helper.callerClass(@""));
        var titleTokens = jobj.SelectTokens("...title");
        var values = titleTokens.Select(x => (x as JProperty).Value);

        if (titleTokens.Contains(s))
        {
            return true;
        }
        else
        {
            return false;
        }
    } 

}