constants in global.asa

Discussion in 'ASP General' started by 2obvious, Feb 4, 2005.

  1. 2obvious

    2obvious Guest

    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?
     
    2obvious, Feb 4, 2005
    #1
    1. Advertising

  2. 2obvious

    Evertjan. Guest

    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)
     
    Evertjan., Feb 4, 2005
    #2
    1. Advertising

  3. 2obvious

    2obvious Guest

    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...
     
    2obvious, Feb 4, 2005
    #3
  4. 2obvious

    Patrice Guest

    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" <> a écrit dans le message de
    news:...
    > 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?
    >
     
    Patrice, Feb 4, 2005
    #4
  5. 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.
     
    Bob Barrows [MVP], Feb 4, 2005
    #5
  6. 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.
     
    Bob Barrows [MVP], Feb 4, 2005
    #6
  7. 2obvious

    Patrice Guest

    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]" <> a écrit dans le message de
    news:...
    > 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.
    >
    >
     
    Patrice, Feb 4, 2005
    #7
  8. 2obvious

    Evertjan. Guest

    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)
     
    Evertjan., Feb 4, 2005
    #8
  9. 2obvious

    Patrice Guest

    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." <> a écrit dans le message de
    news:Xns95F3AB7B9B2FCeejj99@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)
    >
     
    Patrice, Feb 4, 2005
    #9
  10. 2obvious

    Evertjan. Guest

    Patrice wrote on 04 feb 2005 in microsoft.public.inetserver.asp.general:
    > "Evertjan." <> 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)
     
    Evertjan., Feb 4, 2005
    #10
  11. 2obvious

    Patrice Guest

    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
     
    Patrice, Feb 4, 2005
    #11
  12. 2obvious

    Evertjan. Guest

    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)
     
    Evertjan., Feb 4, 2005
    #12
  13. 2obvious

    2obvious Guest

    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?
     
    2obvious, Feb 5, 2005
    #13
  14. 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-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
    --
    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"
     
    Bob Barrows [MVP], Feb 5, 2005
    #14
  15. 2obvious

    2obvious Guest

    >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.
     
    2obvious, Feb 6, 2005
    #15
  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.


    This might help:
    http://blogs.msdn.com/ericlippert/archive/2003/10/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"
     
    Bob Barrows [MVP], Feb 6, 2005
    #16
  17. 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...ver.asp.*&hl=en&lr=&ie=UTF-8&scoring=r&rnum=2

    Also see Appendix 3 of
    http://www.microsoft.com/technet/pr...gies/iis/maintain/optimize/iis5tune.mspx#ECAA
    for details of how the compiled p-code is cached in ASP.
     
    Michael D. Kersey, Feb 7, 2005
    #17
  18. 2obvious

    2obvious Guest

    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...)
     
    2obvious, Feb 7, 2005
    #18
  19. 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.
     
    Bob Barrows [MVP], Feb 7, 2005
    #19
  20. 2obvious

    Patrice Guest

    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" <> a écrit dans le message de
    news:...
    > 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?
    >
     
    Patrice, Feb 7, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darlene Gauthier

    PostRequestHandlerExecute event in global.asa

    Darlene Gauthier, Jul 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,457
    Karl Seguin
    Jul 23, 2003
  2. Kevin Spencer

    Re: global.asa file (count sessions)

    Kevin Spencer, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    Kevin Spencer
    Jul 30, 2003
  3. DevBoy

    Global.asa File ?????

    DevBoy, Aug 12, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    373
    Rick Strahl [MVP]
    Aug 13, 2003
  4. Wayne
    Replies:
    2
    Views:
    500
    Wayne
    Nov 11, 2003
  5. 2obvious

    no global constants in global.asax?

    2obvious, Sep 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,892
Loading...

Share This Page