-1

I need to call a non static method from a static[webmethod]. It is not getting call, I tested it using breakpoints.i tried to call it by making a instance to the class. This is what i am trying .

[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 is name of the class here.The control is going into if and else statements depending upon input,but after that it directly get jump out.Moreover when i hover the mouse over querystring ,it says

Unreachable code detected.

What should I do to make it working?

sll
  • 61,540
  • 22
  • 104
  • 156
user1627138
  • 213
  • 1
  • 7
  • 17
  • 3
    You have a `return` in both the `if` and `else` sections... you'll never reach the next line – freefaller Sep 11 '12 at 12:16
  • also: replace `if (get_ajax_answer_title.Equals("") && (get_ajax_answer_title.Equals("")))` with `if (get_ajax_answer_title.Equals("") && (get_ajax_answer_des.Equals("")))` – Tim Schmelter Sep 11 '12 at 12:17
  • You could have googled this before posting such question it will save every ones time at SO.this should help http://stackoverflow.com/questions/1360183/call-non-static-method-from-static-method-c-sharp. – ankur Sep 11 '12 at 12:18
  • 1
    @ankur That won't help, the cause is completely different. –  Sep 11 '12 at 12:19
  • @hvd oops i didn't check the complete stuff yup freefaller has pointed out the thing which needs to be changed . – ankur Sep 11 '12 at 12:21

8 Answers8

2

That's because you return from both halves if the preceding if statement.

There is no way for it to get up to that line.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

It's because you have a return statement in both the IF and ELSE section.

So regardless of the result of the conditional; you never get below that.

Jim B
  • 8,344
  • 10
  • 49
  • 77
1

Your method ends after the if statement, wether it is true (return "null") or not (return "inserted"). So your code that is after the if statement (where you create the query string) can never be executed.

Jan
  • 2,168
  • 2
  • 19
  • 28
0

Your problem is that you are exiting your method in both your if and else clauses. Your code is essentially:

MyMethod() 
{
    if (someCondition)
        return
    else
        return

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

}
RB.
  • 36,301
  • 12
  • 91
  • 131
0
querystring object_new = new querystring();
object_new.show();

part will never be reached because in both of your block statement in your condition you wrote a return.

Max
  • 3,453
  • 3
  • 32
  • 50
0

Yes, that is because you have return statements at the end of both your if and else blocks.

Change it to

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

}

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
0

Unreachable code detected. is because both paths of your if statement return early.

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

You have answered your original question correctly, i.e. Instantiate an instance of a non-static method to be able to call a method on it.

querystring object_new = new querystring();
object_new.show();
StuartLC
  • 104,537
  • 17
  • 209
  • 285
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;
       }
  • It's not a good idea to just put code... for one thing, it's not always obvious what your change is, and it's always better to give a reason for the change – freefaller Sep 11 '12 at 12:20