469,314 Members | 2,224 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,314 developers. It's quick & easy.

constants in global.asa

I've been trying to create read-only global variables by creating
constants (Const) in my global.asa, but I can't seem to reference them.
Sticking them in an include works fine, but it seems more structurally
sound to use Application_OnStart. Am I attempting the impossible, and
if so, why?

Jul 22 '05 #1
20 4236
2obvious wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
I've been trying to create read-only global variables by creating
constants (Const) in my global.asa, but I can't seem to reference them.
Sticking them in an include works fine, but it seems more structurally
sound to use Application_OnStart. Am I attempting the impossible, and
if so, why?


Use application variables.

Constants are vbscript [or other script] and die with the page.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #2
What about using the Public declaration in the global.asa? (uh, Can
you use a Public declaration with a constant--isn't Const a declaration
as well?) From what I read, Public cannot be used within a procedure,
so I would have to put it outside Application_OnStart, but it would
still be in the global.asa...

Jul 22 '05 #3
AFAIK you can declare objects but not VBScript constants..

My personal preference is to include a single file in all my ASP Pages that
contains in tunrs includes the most frequently used stuff such as constants,
UI utilities, database access etc...
In each page I can include sometimes more files with very specialized needs.

This way I generally just have to care about a single include file...

Patrice
--

"2obvious" <va********@hotmail.com> a écrit dans le message de
news:11*********************@o13g2000cwo.googlegro ups.com...
I've been trying to create read-only global variables by creating
constants (Const) in my global.asa, but I can't seem to reference them.
Sticking them in an include works fine, but it seems more structurally
sound to use Application_OnStart. Am I attempting the impossible, and
if so, why?

Jul 22 '05 #4
2obvious wrote:
What about using the Public declaration in the global.asa? No (uh, Cany ou use a Public declaration with a constant--isn't Const a
declaration as well?) From what I read, Public cannot be used within
a procedure, so I would have to put it outside Application_OnStart,
but it would still be in the global.asa...


No!
You have to put it into an application variable if you want other pages to
see it!
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #5
Patrice wrote:
AFAIK you can declare objects but not VBScript constants..


That's wrong too.

const cText = "This is constant text"

is a perfectly valid statement.

You just can't put a const into an application variable.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #6
Ok, I actually meant a constant that is public to the whole application
which is what Evertjan is after....

It left finally us with two options :
- using Application variables
- using an include file

Patrice
--

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> a écrit dans le message de
news:uF**************@TK2MSFTNGP12.phx.gbl...
Patrice wrote:
AFAIK you can declare objects but not VBScript constants..


That's wrong too.

const cText = "This is constant text"

is a perfectly valid statement.

You just can't put a const into an application variable.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Jul 22 '05 #7
Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
Ok, I actually meant a constant that is public to the whole application
which is what Evertjan is after....

It left finally us with two options :
- using Application variables
- using an include file


As far as I am concerned, in modern programming a constant is just a
variable that is set only once.

In ancient cybertime, often and especially in compiled programs, a constant
was kept in programming memory and a variable in a part of the memory that
could be dynamicly reassigned, taking a lot of processor time, but saving
expensive memory space. Then having seperate constants was a bonus.

Nowadays the idea of a constant is more it's global use, but a variable
will do, if the programmer is not too stupid.

Having seperate constants in vbscript is just nostalgia and useful for the
same stupidity in the context of debugging[, as is option explicit].
The runtime usefullness is zero.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #8
Choose the method that best fit in ASP.

You probably meant the other way round. Constants are still there and should
be used when needed (they guarantee you can't update them). They are
available in .NET...

Patrice
--

"Evertjan." <ex**************@interxnl.net> a écrit dans le message de
news:Xn********************@194.109.133.29...
Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
Ok, I actually meant a constant that is public to the whole application
which is what Evertjan is after....

It left finally us with two options :
- using Application variables
- using an include file

As far as I am concerned, in modern programming a constant is just a
variable that is set only once.

In ancient cybertime, often and especially in compiled programs, a

constant was kept in programming memory and a variable in a part of the memory that
could be dynamicly reassigned, taking a lot of processor time, but saving
expensive memory space. Then having seperate constants was a bonus.

Nowadays the idea of a constant is more it's global use, but a variable
will do, if the programmer is not too stupid.

Having seperate constants in vbscript is just nostalgia and useful for the
same stupidity in the context of debugging[, as is option explicit].
The runtime usefullness is zero.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #9
Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
"Evertjan." <ex**************@interxnl.net> a ‚crit dans le message de
As far as I am concerned, in modern programming a constant is just a
variable that is set only once.

In ancient cybertime, often and especially in compiled programs, a constant
was kept in programming memory and a variable in a part of the memory
that could be dynamicly reassigned, taking a lot of processor time,
but saving expensive memory space. Then having seperate constants was
a bonus.

Nowadays the idea of a constant is more it's global use, but a
variable will do, if the programmer is not too stupid.

Having seperate constants in vbscript is just nostalgia and useful
for the same stupidity in the context of debugging[, as is option
explicit]. The runtime usefullness is zero.


Choose the method that best fit in ASP.

You probably meant the other way round.


No.
Constants are still there and
should be used when needed (they guarantee you can't update them).
As I said, Constants used to have a runtime advantage,
now they have ony a limited [sorry I said zero] use in the programming
phase.

I don't thing that use is important,
so I never use a contant anymore in ASP vbs.
They are available in .NET...


Perhaps, but strictly that is off topic on this classic asp NG.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #10
Ok, this is not my option. I found them usefull and actually it's likely you
are using some of them (such as some ADO, VBScript,
Scripting.FileSystemObject etc...) even if I admit you need to declare them
less frequently.

If you have to define some constant values in your VBScript code library, I
would definitely recommend using constants rather than variables that could
be changed "by accident".

Patrice
Jul 22 '05 #11
Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
Ok, this is not my option. I found them usefull and actually it's
likely you are using some of them (such as some ADO, VBScript,
Scripting.FileSystemObject etc...) even if I admit you need to declare
them less frequently.

If you have to define some constant values in your VBScript code
library, I would definitely recommend using constants rather than
variables that could be changed "by accident".


[Please Patrice, this is not email, but usenet, and others perghaps are not
able to remember the last posting, so please quote relevant parts. Its
netiquette]

Thank you for your recommendation.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #12
Okay, this is what I'm getting:

1) Simply sticking a variable, object, constant, etc. inside the
global.asa isn't enough to make it global. I /must/ be instantiated
inside a procedure.

2) The whole question of whether "Public" and "Const" can be used at
the same time is mute, because you can't have a VBscript constant with
a scope any greater than page scope.

3) VBscript user-created constants are a legacy coding technique.

I can buy that. Especially considering the troubles I've had trying to
translate some of my ASP over to ASP.NET.

Patrice said:
[Constants] are available in .NET...


--are you /sure/? I certainly couldn't find them. I'm talking
specifically about ASP.NET, not the whole VB.NET language. 'Best I
could do was read-only variables.

But Evertjan. is right. Sticking to ASP issues: really, I only use
constants to prevent accidentally changing variables that are...well,
constant. So a new spin on this question:

Is there a way to create read-only variables with application scope in
the Application_OnStart procedure?

Jul 22 '05 #13
2obvious wrote:
Okay, this is what I'm getting:

1) Simply sticking a variable, object, constant, etc. inside the
global.asa isn't enough to make it global. I /must/ be instantiated
inside a procedure.
No. it must be put into either a Session or Application variable in order to
make it global to all pages. This can be done anywhere: in global.asa, or in
any of the pages in the application, either inside or outside a procedure.
In global.asa:

Sub Application_onstart
Application("globalvar") = "test"
end sub
in test.asp:
<%
Response.Write Application("globalvar")
%>


2) The whole question of whether "Public" and "Const" can be used at
the same time is mute, because you can't have a VBscript constant with
a scope any greater than page scope.
True

3) VBscript user-created constants are a legacy coding technique.

I can buy that. Especially considering the troubles I've had trying
to translate some of my ASP over to ASP.NET.

I'm not so sure I buy that. When constants are used, the constant values are
written into the compiled code at compile-time. When variables are used,
memory is allocated to contain the values that will be supplied at runtime.
I don't understand why that amounts ot "zero value".
Patrice said:
[Constants] are available in .NET...
--are you /sure/? I certainly couldn't find them.


1. vbscript can still be used in an aspx page. vbscript doesn't lose the
Const keyword just because of where its being used
2. http://msdn.microsoft.com/library/en...vastmConst.asp
I'm talking
specifically about ASP.NET, not the whole VB.NET language. 'Best I
could do was read-only variables.

But Evertjan. is right. Sticking to ASP issues: really, I only use
constants to prevent accidentally changing variables that are...well,
constant. So a new spin on this question:

Is there a way to create read-only variables with application scope in
the Application_OnStart procedure?


No. There is no such thing as a read-only variable. If that is truly what
you want, then you need to use include files or classes.

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #14
>When constants are used, the constant values are
written into the compiled code at compile-time.
When variables are used, memory is allocated to
contain the values that will be supplied at runtime.
I thought Active Server Pages were interpreted, not compiled.
There is no such thing as a read-only variable. If
that is truly what you want, then you need to use
include files or classes.


Wait! --And Application variables can't be arrays or objects, they're
just your run-of-the-mill untyped variable? Hmm.

Jul 22 '05 #15
2obvious wrote:
When constants are used, the constant values are
written into the compiled code at compile-time.
When variables are used, memory is allocated to
contain the values that will be supplied at runtime.
I thought Active Server Pages were interpreted, not compiled.


This might help:
http://blogs.msdn.com/ericlippert/ar.../21/53264.aspx
There is no such thing as a read-only variable. If
that is truly what you want, then you need to use
include files or classes.


Wait! --And Application variables can't be arrays or objects, they're
just your run-of-the-mill untyped variable? Hmm.


No, they can be arrays. They can even be objects. However, just because you
can put an object in application or session does not mean that you should.
Only free-threaded objects should be put in application or session.
Otherwise, your application becomes thread-bound, which serializes all uses
of the object, killing performance.
http://www.aspfaq.com/2053

Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 22 '05 #16
2obvious wrote:
When constants are used, the constant values are
written into the compiled code at compile-time.
When variables are used, memory is allocated to
contain the values that will be supplied at runtime.

I thought Active Server Pages were interpreted, not compiled.


No. VBScript/Jscript pages are compiled to a pseudo-code (p-code) and
then that p-code is interpreted.

Read this post to see an example of the p-code:
http://groups-beta.google.com/group/...oring=r&rnum=2

Also see Appendix 3 of
http://www.microsoft.com/technet/pro...tune.mspx#ECAA
for details of how the compiled p-code is cached in ASP.
Jul 22 '05 #17
Wow. Given its ramifications, the global.asa file is now seeming
pretty useless.

After all, Application variables can be created anywhere. And from the
looks of things, Session variables are only used by "lazy" programmers.

I can say that, because I happen to be one of those lazy programmers.
I'm maintaining an application that wasn't very well planned out.
Consequently, I end up having to create new data structures after the
fact. Most of this data should be stored in objects, but as things
stand there are a bazillion variables passed in hidden form fields,
which is a royal headache to trace.

....But better for me to do the tracing than to dump this burden on the
server, right?

(...I can't wait to finally get this .NET thing figured out...)

Jul 22 '05 #18
2obvious wrote:
Wow. Given its ramifications, the global.asa file is now seeming
pretty useless.

After all, Application variables can be created anywhere.
True, but single-initiation variables should be initiated during
application_onstart. Other pages should only be setting application
variables if the value for the variable either needs to change during the
loading of the pages, or the value is not known during onstart.
And from
the looks of things, Session variables are only used by "lazy"
programmers.
No! Not true at at all! Storing scalar values in session or application is
an excellent way to cache them. Avoiding I/O or database access is still a
laudable goal. Yes, they can be over-used by "lazy" programmers, but their
use does not necessarily indicate laziness.

I can say that, because I happen to be one of those lazy programmers.
I'm maintaining an application that wasn't very well planned out.
Consequently, I end up having to create new data structures after the
fact. Most of this data should be stored in objects, but as things
stand there are a bazillion variables passed in hidden form fields,
which is a royal headache to trace.


As I said, arrays are still do-able. You may also want to look into the XML
Document, an object which IS free-threaded and therefore suitable to being
stored in Application/Session

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 22 '05 #19
In VB.NET this is still the "Const" keyword (I don't see what you meant by
"specifically ASP.NET", ASP.NET uses anyway VB.NET, C# or some other .NET
language).

Here it looks Evertjan is not really after constants but after configuration
variables that are defined outside the application an read only inside in
the application.

I see basically two options :
- constants once again (having for example a const.asp editable file with
all constants for the app)

Const App_Path=\\myserver\folder\whatever

Granted for some reason Id on"t really understand, this is not wanted (don't
want include files ?)

Another option would to use VBScript classes. If the class only has a
proprety get the property can be read but can't be set. Unfortunately
support for classes is quite light and you'll still need include fields
unless you want to type the same things over and over again...

Patrice
--

"2obvious" <va********@hotmail.com> a écrit dans le message de
news:11**********************@f14g2000cwb.googlegr oups.com...
Okay, this is what I'm getting:

1) Simply sticking a variable, object, constant, etc. inside the
global.asa isn't enough to make it global. I /must/ be instantiated
inside a procedure.

2) The whole question of whether "Public" and "Const" can be used at
the same time is mute, because you can't have a VBscript constant with
a scope any greater than page scope.

3) VBscript user-created constants are a legacy coding technique.

I can buy that. Especially considering the troubles I've had trying to
translate some of my ASP over to ASP.NET.

Patrice said:
[Constants] are available in .NET...


--are you /sure/? I certainly couldn't find them. I'm talking
specifically about ASP.NET, not the whole VB.NET language. 'Best I
could do was read-only variables.

But Evertjan. is right. Sticking to ASP issues: really, I only use
constants to prevent accidentally changing variables that are...well,
constant. So a new spin on this question:

Is there a way to create read-only variables with application scope in
the Application_OnStart procedure?

Jul 22 '05 #20
Patrice wrote on 07 feb 2005 in microsoft.public.inetserver.asp.general:
Here it looks Evertjan is not really after constants but after
configuration variables that are defined outside the application an
read only inside in the application.


I wouldn't know where I was after.
Certainly not something "external" to an apllication.
I am not the OP, I thought you where, Patrice?

The only point I wanted to make was, that constants used to have a
compiling advantage in the old days,
and nowadays they only are an additional bonus against programming
mistakes, the usefulness of which is very limited in small scale
programming on an asp page.

In larger programming tasks however,
seperate constants and also option explicit are very usefull.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 22 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Amadelle | last post: by
8 posts views Thread by Marty | last post: by
1 post views Thread by 2obvious | last post: by
3 posts views Thread by www.gerardvignes.com | last post: by
54 posts views Thread by shuisheng | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.