Calling a variable from a block

Discussion in 'C#' started by cancer10, Sep 19, 2008.

  1. #1
    I have this ASP code

    
    <%
    sub mysub
    mytestvariable = 111
    end sub
    
    
    call mysub
    response.write mytestvariable
    
    %>
    Code (markup):
    Why does the above code show BLANK value when I response.write mytestvariable ? Shouldn't it output 111?


    Whats the solution?
     
    cancer10, Sep 19, 2008 IP
  2. wacamoi

    wacamoi Peon

    Messages:
    810
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try this
    complete your works in sub not off sub
    <%
    sub mysub
    mytestvariable = 111
    response.write mytestvariable
    end sub


    mysub


    %>
     
    wacamoi, Sep 19, 2008 IP
  3. mintoj

    mintoj Peon

    Messages:
    317
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It's because the variable mytestvariable is in a local code block and only has scope in that code block. You need to declare it outside of the block.

    The correct way to do it would be to use a function with a return value:

    function myfunction()
    mytestvariable = 111
    return mytestvariable
    end function

    Response.Write(myfunction())
     
    mintoj, Sep 20, 2008 IP