Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring global a variable inside a function

Status
Not open for further replies.

Faheemi

Programmer
Sep 2, 2001
59
HK
Hi there friends,

I want to be able to declare a variable inside a Function or Sub and want to access it in the other part of asp file. Is this is possible? I am using VbScript

Thanks for your help

Hameed
 
You need only declare it as a
Code:
Public
variable.

Instead of
Code:
    Dim myVariable
or
Code:
    Private myVariable
you use
Code:
    Public myVariable
The variable will then be available throughout the entire script.
 
If I assign like the following I am getting a syntax error message.

<%
Public a
Function test1()
a = 1000
End Function

Response.Write( &quot;a : &quot; & a & &quot;<br>&quot; )
test1()
Response.Write( &quot;a : &quot; & a & &quot;<br>&quot; )

%>


Please help. Thanks for your help

Hameed
 
I copied and pasted your code and had no problem, getting
Code:
a : 
a : 1000
as a result. What line is the syntax error on?

Technically when you call the function if you're not explicitly using the
Code:
Call
statement then you shouldn't use the parentheses, so you should either write it as
Code:
Response.Write( &quot;a  : &quot; & a & &quot;<br>&quot; )
test1
Response.Write( &quot;a  : &quot; & a & &quot;<br>&quot; )
or
Code:
Response.Write( &quot;a  : &quot; & a & &quot;<br>&quot; )
Call test1()
Response.Write( &quot;a  : &quot; & a & &quot;<br>&quot; )
 
Sorry,

I wanted to paste the following code but I pasted the other code wrongly. Please advise the following code is allowed...

<%
Function test1()
Public a
a = 1000
End Function

Response.Write( &quot;a : &quot; & a & &quot;<br>&quot; )
test1()
Response.Write( &quot;a : &quot; & a & &quot;<br>&quot; )

%>

I am getting the following error:

Microsoft VBScript compilation (0x800A03EA)
Syntax error
/test.asp, line 4

Thanks

Hameed


 
Ah, I see.

Well, I thought you could, but you clearly cannot. I would think there's a way to move the variable up/out in scope but I don't know how.

Sorry I wasn't more help.
 
hmm.. well yes there is no public, private declarations in classic asp. from what I read all you want to do is declare a var in a function and then have access to it in other functions? sounds fairly simple of a idea with a bunch of possiblilities.
one pass it as a parameter, second give the value to the var by means of another revolving variable

Dim c
function this
dim a
a = &quot;that&quot;
c = a
call that
end function

function that
dim b
b = &quot;that&quot;
c = c+b
end function

response.write c 'output thisthat


then parameter passing

call this

function this
dim a
a = &quot;that&quot;
that(a)
end function

function that(val)
dim b
b = a & &quot;that&quot;
end function

response.write b 'output thisthat



____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
It's really hard to keep it all straight when you write VB, VBA, VBScript outside ASP, and VBScript inside ASP. Ugh.
 
onpnt,
Your examples don't actually work! The first one is OK but you just missed out the call to this() before Response.Writing c.

The second one fails because all your variables are declared within functions so you have no access to them in the main script body (ie Response.Write b).

Which actually answers the original question:
I want to be able to declare a variable inside a Function or Sub and want to access it in the other part of asp file. Is this is possible?

Answer: NO!

The funny thing is that Faheemi actually answered his own question by mis-copying his example. To be able to access a variable in the main body, it has to be declared in the main body. Any variable declared inside of a function only has scope for that function.

So you need to declare the variable outside of any function, even though you may then access that variable inside a function later on.

Code:
Option Explicit

Dim myvar

Response.Write &quot;myvar before call to sub: &quot; & myvar & &quot;<br />&quot;

Call mysub()

Response.Write &quot;myvar after call to sub: &quot; & myvar & &quot;<br />&quot;

Sub mysub()
  myvar = &quot;value assigned in sub&quot;
End Sub

--James
 
from now on I need remember the suedocode line more often. [wink]

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
alright, jameslean is out for onpnt today [rofl] grammatically and pragmatically

____________________________________________________
get the best answer to your questions by asking the best questions &quot;General FAQ&quot; faq333-2924
onpnt2.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top