constants in global.asa

2

2obvious

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?
 
E

Evertjan.

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.
 
2

2obvious

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...
 
P

Patrice

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
 
B

Bob Barrows [MVP]

2obvious said:
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!
 
B

Bob Barrows [MVP]

Patrice said:
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.
 
P

Patrice

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
 
E

Evertjan.

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.
 
P

Patrice

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. said:
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.
 
E

Evertjan.

Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
Evertjan. said:
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.
 
P

Patrice

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
 
E

Evertjan.

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.
 
2

2obvious

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?
 
B

Bob Barrows [MVP]

2obvious said:
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-us/vblr7/html/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
 
2

2obvious

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.
 
B

Bob Barrows [MVP]

2obvious said:
I thought Active Server Pages were interpreted, not compiled.

This might help:
http://blogs.msdn.com/ericlippert/archive/2003/10/21/53264.aspx
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
 
M

Michael D. Kersey

2

2obvious

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...)
 
B

Bob Barrows [MVP]

2obvious said:
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
 
P

Patrice

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 said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top