...

View Full Version : Sub Rountine



charon
01-28-2003, 06:08 AM
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!

bostjank
01-28-2003, 07:17 PM
Hi!

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

Bostjan

whammy
01-29-2003, 02:29 AM
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 %>

:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum