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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variable problem

Status
Not open for further replies.

Crundy

Programmer
Jul 20, 2001
305
GB
Hello,
I know this is a really dim question but I've looked everywhere with no luck.

How do I define a variable as public so the scope will include a function? I've tried:

<%Public myName
myName = &quot;Crundy&quot;
PrintName()%>

Some HTML

<%function PrintName()
response.write(myName)
end function%>

But I get a syntax error on the &quot;Public&quot; line. The functions can't read the variable if I don't define it. C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
try using a Session variable - this will make the variable accessible throughout your entire site for the duration of the user visit

Session(&quot;myName&quot;) = &quot;Crundy&quot; Tony
reddot.gif WIDTH=400 HEIGHT=2 VSPACE=3

 
Is there an easier way to do it? I want to reduce the amount of cookies & session variables I'm posting to the user.

Also, I just want to define the variable in my config script (which is included in all other scripts) for use in a function in only one other page. Therefore it seems excessive to declare it as a session variable.

Any ideas? Surely there is a way to define a variable as global? (speaking of which - I'm not using a global.asa by the way). C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
Just place this line of code at the top of your page or in an include file

Dim myName

It will then be visible to all the code in your asp page and any include files. In fact, you should declare this variable in the config script you mention. That way it is available to all asp files that include it.

If this is what you have done and it is not working, post and example of your asp file and how you are using the variable. Include the asp and the config script files.

Thanks,

Gabe
 
Already tried that and it still didn't work.

The thing I don't get is that I checked loads of documentation that says you can just use:

Public myValue

But I get a syntax error when I use it!

I wimped out in the end and passed the variables to the function thus:

myFunction (value1, value2)

If you are interested, here's the sort of thing I had:

Config.asp:
dim myValue
myValue = &quot;Crundy&quot;

otherfile.asp:
<!--#include file=&quot;config.asp&quot;-->
<%printName(&quot;Hello&quot;)%>
Some html
<%function printName(greeting)
response.write(greeting & &quot; &quot; & myValue)
end function%>

So like I say, I gave up in the end and changed otherfile.asp to:
<!--#include file=&quot;config.asp&quot;-->
<%printName(&quot;Hello&quot;, myValue)%>
Some html
<%function printName(greeting, myValue)
response.write(greeting & &quot; &quot; & myValue)
end function%>


I would still be interested if anyone knows why I'm getting a syntax error when I use Public instead of Dim. Is it because I'm not using Option Explicit? C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
your original 'otherfile.asp' was making a call to the function printName BEFORE you had actaully declared the function.

How about putting the function printName in your config.asp file, or just place it about any other reference to it Tony
reddot.gif WIDTH=400 HEIGHT=2 VSPACE=3

 
OK I put them the wrong way round in that example, but that wasn't the problem. It was just about defining a variable as public.

Good idea about putting the function into the config script though. Perhaps I should've done that instead. C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
OK, I see your problem.

In the config.asp page you need to wrap your code in <% %>. I tried this and the code works just fine.

As to your question about using Public instead of Dim.
Public is not used in an ASP page unless you create a class in the ASP page.

Ex.

Class clsDoSomeWork
Public Results
Public Function DoSomething(Instructions)

End Function
End Class

set oWorking = new clsDoSomeWork


 
That wasn't the actual config.asp file. I did have <%'s round the code in the original.

So you can't use 'public' unless it's in a class? OK I get it. Thanks. C:\DOS:>
C:\DOS:>RUN
RUN DOS RUN!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top