locked
Global variables/functions RRS feed

  • Question

  • User-1004389867 posted

    Hello,

    I'm building a web application in .net 4.0:

    I have multiple variables with query strings that I use in multiple pages, and I would like to declarate them only once, without do copy-paste for everything local where I use them.

    There's any simple and effective way to do this? I 'hear' talk much about static classes and variables, I have already tried to create my own but I can't call them from the .aspx or aspx.vb files. (gives me this error: 'class/variable' is not declared. It may be inaccessible due to its protection level.)

    I also use many repeated code, so I would like to know if I can put all common functions in a single file.

    Thanks. 

    Saturday, January 29, 2011 6:40 AM

Answers

  • User-693248168 posted

    Hi,


    You can use Application object for the above scenario.

    But you will have to create them every time your application starts.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 9:50 AM
  • User-1412735316 posted

    if you want to use any static variable globaly then i think the easiest way is to declare at web.config file. you can access from aspx or aspx.vb page easily just by using configurationmanager.

    you may check the following links

    http://stackoverflow.com/questions/1797568/how-to-create-global-variables-asp-net-global-asax

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=126&AspxAutoDetectCookieSupport=1

    http://dotnetperls.com/global-variables-aspnet

    http://www.devasp.net/net/articles/display/323.html

    you can also try Global.asax

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 10:04 AM

All replies

  • User2019981500 posted

    Hi,

    If you have Vb.Net based web Application you can do it using Module

    like you need to declare this type of variable as a Public scope in a module somewhere in your program.

    Module modVar2
        Public gintValue As Integer
        
        Public Sub GlobalModuleValue()
         Response.write( _
             "gintValue = " & gintValue)
        End Sub
    End Module
    


    Now if you are use C# based web application you can simple declare BaseClass with all your member and methods and inherit them in derived classes

    like

    global variables in c#.net - Stack Overflow


    Regards

    Shabir

    Saturday, January 29, 2011 9:42 AM
  • User-693248168 posted

    Hi,


    You can use Application object for the above scenario.

    But you will have to create them every time your application starts.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 9:50 AM
  • User-1412735316 posted

    if you want to use any static variable globaly then i think the easiest way is to declare at web.config file. you can access from aspx or aspx.vb page easily just by using configurationmanager.

    you may check the following links

    http://stackoverflow.com/questions/1797568/how-to-create-global-variables-asp-net-global-asax

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=126&AspxAutoDetectCookieSupport=1

    http://dotnetperls.com/global-variables-aspnet

    http://www.devasp.net/net/articles/display/323.html

    you can also try Global.asax

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 29, 2011 10:04 AM
  • User-1004389867 posted

    Hello,

    Thanks the variables are fixed, I'm using web.config with the tag appSettings.

    However the global functions are still not working...

    Hi,

    If you have Vb.Net based web Application you can do it using Module

    like you need to declare this type of variable as a Public scope in a module somewhere in your program.


    I tried your code (replaced the default code of a vb.class that is localized in the root of the website) but seems I can't call their fuctions from an aspx page or aspx.vb

    Should I register the module or something to call it, or I just did it wrong placing it the vb class.


    Thanks to all.
     


     

    Saturday, January 29, 2011 2:19 PM
  • User-1412735316 posted

    nice to hear that you picked config file option.

    Sunday, January 30, 2011 2:03 PM
  • User-1004389867 posted

    I resolved partially the global functions problem, putting them in the masterpage[in code page].

    Although not all functions work, for example the ones that use the class Response. (which don't allow me to put it in shared/static functions)

    To call them I used:  ASP.site_master.function()

    Tuesday, February 1, 2011 7:54 PM
  • User1564403742 posted

    check this out:

    how-to-create-global-functions-in-asp-net-4-0/

    Sunday, June 10, 2012 9:50 AM