locked
Jump and goto RRS feed

  • Question

  • Hello,

    I want to use goto command to jump my code to the bottom. Thanks.

    protected void Page_Load(object sender, EventArgs e)
            {
    		 Response.Redirect("~/Login.aspx",false);
    		  if (!IsPostBack)
                {
    			 \\ blah 
    			 }
    			 else
    			 {
    			 \\blah
    			 }
    		\\ Bottom
    		} 

    After executing Response.Redirect("~/Login.aspx",false) then goto bottom.

    Thanks.

    Thursday, April 12, 2012 7:08 PM

Answers

  • The Page_Load method returns void - you don't actually have to return anything. Just have an empty return statement (return;) and it will break out of the method for you.

    Check out My Blog. Now updated to actually work!

    • Marked as answer by ardmore Friday, April 13, 2012 6:06 PM
    Friday, April 13, 2012 1:48 PM

All replies

  • Then just make IsPostBack true? Cannot see any need for a goto anywhere. Or throw the entire if..else.. away since if you jump over it, it is redundant. Am I missing something or have you missed out something?

    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, April 12, 2012 7:32 PM
  • There is a slight difference. Either IsPostBack true or false block there are a lot of code. When the code goes to Response.Redirect("~/Login.aspx",false);I want to code jumps to the last curly bracket immediately then goto login page.
    Thursday, April 12, 2012 8:03 PM
  • This is your code:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Redirect("~/Login.aspx",false);
        if (!IsPostBack)
        {
           \\ blah
        }
        else
        {
           \\blah
        }
        \\ Bottom
    }

    This appears to do what you ask:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Redirect("~/Login.aspx",false);
    }

    Apart from going to login page which is not shown anywhere in your code.

    No what am I missing?


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, April 12, 2012 8:14 PM
  • Maybe I am wrong. But when the page loads, IsPostBack is false, then after executing Response.Redirect, the code will goto the block

    if (!IsPostBack)
    {
        \\ The code will go to here, it is not expected. 
    }
     

    Thursday, April 12, 2012 8:21 PM
  • You asked:

       After executing Response.Redirect("~/Login.aspx",false) then goto bottom.

    That is exactly what the second bit of code does, it jumps every thing and goes to bottom (which means you can eliminate every thing after the Redirect).

    IsPostBack seems to be a variable, if it is a property of response then it needs to be something like Response.IsPostBack or what ever you are testing for being a post back.

    Now either I am misunderstanding what you want to do, or you are not properly saying what you want to do. If you want to unconditionally jump over a block of code (which is what goto does) then you can just delete that block of code. If you want to jump over a block conditionally, then you need to use an if and to get the condition right. If you want to do a block when a condition is true and some other block when it is not, you need an if...else... So what is it you want to do?


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, April 12, 2012 8:39 PM
  • Read here about goto statement. I dont recomment to use it.

    Mitja

    Thursday, April 12, 2012 8:53 PM
  • The original code is:

     protected void Page_Load(object sender, EventArgs e)
     {
          if (mode == something)
          {
               Response.Redirect("~/Login.aspx");
          }
           if (!IsPostBack)
          {
               \\ blah
          }
          else
          {
              \\ blah
          }
     }
    I can't delete that block of code.
    Thursday, April 12, 2012 8:56 PM
  • If you ever find yourself wanting to use goto, it means something is wrong. You should never need it in C#.

    In your case, why don't you just use return? Or, use else if?

    If you want to break out of the method entirely after Response.Redirect:

    if (mode == something)
    {
        Response.Redirect("~/Login.aspx");
        return;
    }

    If you have some code after your else that you need to be executed, just use else if:

    if (mode == something)
    {
        Response.Redirect("~/Login.aspx");
    }
    else if (!IsPostBack)
    {
        // Do Something
    }
    else
    {
        // Do something else
    }
    
    // Code here will still get hit


    Check out My Blog. Now updated to actually work!

    Thursday, April 12, 2012 9:49 PM
  • I can't delete that block of code.

    Which block? There are three in the method: if(mode..){...}, if(!IsPostBack){...} and else{...}. Unless you count the Page_Load(...){...} as a block as well, in which case there are four.

    Why can it not be deleted? What stops you deleting it?


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Thursday, April 12, 2012 10:26 PM
  • If you ever find yourself wanting to use goto, it means something is wrong. You should never need it in C#.

    In your case, why don't you just use return? Or, use else if?

    If you want to break out of the method entirely after Response.Redirect:

    if (mode == something)
    {
        Response.Redirect("~/Login.aspx");
        return;
    }

    If you have some code after your else that you need to be executed, just use else if:

    if (mode == something)
    {
        Response.Redirect("~/Login.aspx");
    }
    else if (!IsPostBack)
    {
        // Do Something
    }
    else
    {
        // Do something else
    }
    
    // Code here will still get hit


    Check out My Blog. Now updated to actually work!

    It is an asp.net load web page, how can I return it?
    Thursday, April 12, 2012 11:14 PM
  • VB has many more break statements.  Code what you want to do in VB and then use one of the converters or reflectors to convert it to C#.
    Friday, April 13, 2012 1:59 AM
  • This is not possible as the compiler will throw error stating "No such label within the scope of the goto statement"

    Here the goto label is inside if block, which is outsie the scope of expected goto statement.

    Response.Redirect("~/Login.aspx",false); goto label1; if (!IsPostBack) {

    } else { } \\ Bottom label1: statements.. }



    Resolving n Evolving in C# (http://jeanpaulva.com)

    Friday, April 13, 2012 8:16 AM
  • Jean

    Which is the same as this since the goto is an unconditional jump (as I'm sure you know):

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Redirect("~/Login.aspx",false);
        statements....
    }

    Which is why I've tried to get the OP to explain just what they are trying to do. :)


    Regards David R
    ---------------------------------------------------------------
    The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones.
    Object-oriented programming offers a sustainable way to write spaghetti code. - Paul Graham.
    Every program eventually becomes rococo, and then rubble. - Alan Perlis
    The only valid measurement of code quality: WTFs/minute.

    Friday, April 13, 2012 8:28 AM
  • The Page_Load method returns void - you don't actually have to return anything. Just have an empty return statement (return;) and it will break out of the method for you.

    Check out My Blog. Now updated to actually work!

    • Marked as answer by ardmore Friday, April 13, 2012 6:06 PM
    Friday, April 13, 2012 1:48 PM
  • Or you could just put in `response.redirect("...", true)` and you'll redirect immediately, rather than continuing in code despite the redirect.
    Friday, April 13, 2012 3:28 PM