www.webdeveloper.com
Results 1 to 6 of 6

Thread: global variables

  1. #1
    Join Date
    Sep 2003
    Posts
    650

    global variables

    I have a variable called my error


    now I am calling a function called getmycode --and in that function is specific cases I set myerror=to the error

    but I noticed the main code is not reading that error. How can I get it to pass back to the main code?

  2. #2
    Join Date
    Feb 2003
    Posts
    2,745
    Well, the general syntax should be like below, but would need to see your code to say what you're doing wrong
    Code:
    myerror = getmycode()
    
    Function getmycode()
        getmycode = SomeValue
    End Function

  3. #3
    Join Date
    Sep 2003
    Posts
    650
    no my code is as follows (I need to pass back teh code -- the error is just in case an error happened while getting it and I can't return it as the code variable or I won't know it's an error. (and if it's an error I display it in a different place then the code)
    pin="123"
    myerror=""
    mycode=getmycode(pin)

    function getmycode(pin)
    'I get the code from teh db

    if rs.bof and rs.eof then
    myerror="error no such pin in db"
    getmycode=error

    else
    getmycode=rs("code")

    end if




    end function

  4. #4
    Join Date
    Feb 2003
    Posts
    2,745
    It is a scoping issue. Looks like you aren't declaring your variables, so in the getmycode function, myerror is a local variable. Should always declare your vars. The way you have it, passing myerror by reference will solve the problem (see below), but I still reccomend declaring all variables. Option Explicit at the top of your script will help iron these types of issues out.
    Code:
    pin="123"
    myerror=""
    mycode=getmycode(pin, myerror)
    
    function getmycode(byVal pin, byRef myerror)
        'I get the code from teh db
    
        if rs.bof and rs.eof then
            myerror="error no such pin in db"
            getmycode=error
        else
            getmycode=rs("code")
        end if
    end function

  5. #5
    Join Date
    Sep 2003
    Posts
    650
    i did that but it's still not returning myerror to above the function

    how do i return it?

  6. #6
    Join Date
    Feb 2003
    Posts
    2,745
    what db are you using? why check for bof and eof at same time? try just checking eof. need to add some debugging code to see where the problem is -- if the else block executes then myerror will still be empty string. try this
    Code:
    pin="123"
    myerror=""
    mycode=getmycode(pin, myerror)
    
    Response.Write "myerror: " & myerror
    
    function getmycode(byVal pin, byRef myerror)
        'I get the code from teh db
    
        'if rs.bof and rs.eof then
        if rs.eof then
            myerror="error no such pin in db"
            '' What is this next line? should it be getmycode = myerror?
            getmycode=error
            Response.Write myerror
        else
            getmycode=rs("code")
            Response.Write "No Error because we found pin in db"
        end if
    end function

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles