2012-09-11 227 views
-1

我需要从static[webmethod]调用非静态方法。它没有得到调用,我使用breakpoint来测试它。我试图通过给类实例来调用它。 这就是我想要的。如何从静态方法调用非静态方法

[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(); 
    } 

查询字符串是类更换预定控制的名字进入,当且根据输入else语句,但之后它直接拿到跳out.Moreover当我将鼠标悬停在查询字符串,它说

Unreachable code detected. 

我该怎么做才能使它工作?

+3

你有两个'if'和'else'部分......你永远也做不到的下一行 – freefaller

+0

也是'return':更换'如果(get_ajax_answer_title.Equals( “”)&&( get_ajax_answer_title.Equals(“”)))''with'if(get_ajax_answer_title.Equals(“”)&&(get_ajax_answer_des.Equals(“”))' –

+0

你可以在发布这个问题之前用google搜索这个问题,它会节省每一次SO.this应该帮助http://stackoverflow.com/questions/1360183/call-non-static-method-from-static-method-c-sharp。 – ankur

回答

0
[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; 
     } 
+0

这不是一个好主意,只是把代码放在一起......一件事情是,你的改变并不总是显而易见的,并且最好能给出改变的原因 – freefaller

2

这是因为你return从两半,如果前面的if声明。

它没有办法达到那条线。

1

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

所以不管有条件的结果如何;你永远不会低于那个。

0

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

MyMethod() 
{ 
    if (someCondition) 
     return 
    else 
     return 

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

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

部分永远不会因为无论是在你的病情的块语句来达到你写一回。

0

是的,那是因为你在if和else块的末尾都有返回语句。

将其更改为

[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; 

}

0

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(); 
1

你的方法if语句结束后,羯羊它是真实的(return "null")否(return "inserted")。所以你的代码在if语句之后(你创建查询字符串的地方)永远不会被执行。

相关问题