繁体   English   中英

如何从静态方法调用非静态方法

[英]How to call a non static method from static method

我需要从static[webmethod]调用非静态方法。 它没有被调用,我使用breakpoints测试了它。我试图通过创建类的实例来调用它。 这就是我正在尝试的。

[WebMethod]
public static string get_runtime_values(string get_ajax_answer_title,string get_ajax_answer_des)
{
     if (get_ajax_answer_title.Equals("") && (get_ajax_answer_title.Equals("")))
     {
        return "null";
     }
     else
     {
        int got_question_id = getting_question_id;
        DataHandler.breg obj = new DataHandler.breg();
        obj.add_anwers(got_question_id, get_ajax_answer_title, get_ajax_answer_des);
        return "inserted";
     }

     querystring object_new = new querystring();
     object_new.show();
  }

querystring是这里的类的名称。控件将根据输入进入if和else语句,但是之后它直接跳出。此外,当我将鼠标悬停在querystring上时,它说

Unreachable code detected.

我应该怎么做才能使其正常工作?

这是因为如果前面的if语句从两个部分return

它没有办法站起来。

这是因为在IF和ELSE部分中都有return语句。

因此,无论有条件的结果如何; 你永远不会低于这个水平。

您的方法在if语句之后结束,无论它是true( return "null" )还是否( return "inserted" )。 因此,永远不会执行if语句(您在其中创建查询字符串)之后的代码。

您的问题是,您在if和else子句中都退出了方法。 您的代码本质上是:

MyMethod() 
{
    if (someCondition)
        return
    else
        return

    // Any code at this point cannot be executed, because 
    // you have definitely returned from your method.

}
querystring object_new = new querystring();
object_new.show();

永远都无法达到这个部分,因为在您的两个条件下的block语句中,您都写了一个返回。

是的,这是因为在if和else块的末尾都有return语句。

更改为

[WebMethod] 
public static string get_runtime_values(string get_ajax_answer_title,string get_ajax_answer_des) 
{ 
string ret = "null";
 if (!get_ajax_answer_title.Equals("") || (!get_ajax_answer_title.Equals(""))) 
 { 
    int got_question_id = getting_question_id; 
    DataHandler.breg obj = new DataHandler.breg(); 
    obj.add_anwers(got_question_id, get_ajax_answer_title, get_ajax_answer_des); 
    ret = "inserted"; 
 } 

 querystring object_new = new querystring(); 
 object_new.show(); 

return ret;

}

Unreachable code detected. 是因为if语句的两条路径都提前返回。

    if (get_ajax_answer_title.Equals("") && (get_ajax_answer_title.Equals("")))
    {
        return "null"
    }
    else
    {
        return "inserted";
   }
   // Can't get here.

您已经正确回答了原始问题,即实例化非静态方法的实例以能够在其上调用方法。

querystring object_new = new querystring();
object_new.show();
  [WebMethod]
public static string get_runtime_values(string get_ajax_answer_title,string get_ajax_answer_des)
    {  string result;
       if (get_ajax_answer_title.Equals("") && (get_ajax_answer_title.Equals("")))
        {
            result="null";
        }
        else
        {
            int got_question_id = getting_question_id;
            DataHandler.breg obj = new DataHandler.breg();
            obj.add_anwers(got_question_id, get_ajax_answer_title, get_ajax_answer_des);
            result="inserted";
       }
        querystring object_new = new querystring();
        object_new.show();
return result;
       }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM