Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Sub Rountine

  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    317
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sub Rountine

    Hi,

    I have a bit problem on creating sub rountine. Just confused some of the things.

    1.) If the subrountine contain the Global Variables which I need to share all over a page, would it be ok for me not to pass the values. I ask this question is because I found that most of the sub routine do pass the value. For intances:

    Sub Count(Num1, Num2)
    Total = Num 1 + Num 2
    End Sub

    My Sub Rountine is as below:

    Sub AllField()
    i = 1
    Do While InStr(1, AllFields, ",", 1) <> 0
    CommaLoc = InStr(1, AllFields, ",", 1)
    FieldInForm(i) = Trim(Left(AllFields, CommaLoc - 1))
    AllFields = Right(AllFields, Len(AllFields) - CommaLoc)

    i = i + 1
    Loop
    FieldInForm(i) = Trim(AllFields)
    TotalFields = i

    End Sub

    The variable such as FieldInForm(i), CommaLoc will be used outside the Sub routine, except AllFields which just used in Sub-Routine.

    2.) Is there a problem if we din't declare the variable used in the sub routine (as above, the total variable).

    Confusing......

    please advice!

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    The sunny side of the Alps
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi!

    I believe that you must declare a variable outside the procedure if you also want to use it outside the procedure.

    Bostjan

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bostjank is correct. In order to use variables from a subroutine globally, you must do like so:

    <%
    Dim myvariable

    Sub whatever()
    myvariable = "Hi!"
    End Sub

    Call whatever()
    Response.Write(myvariable)
    %>

    I use subroutines in every ASP application I write, the best thing for you to use to make sure you declare variables, etc. is to put this at the top of your page:

    <% Option Explicit %>

    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

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