Using Application() variables in non-web classes

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Using Application() variables in non-web classes

    Hi All,

    I saved a variable to the Application() collection and I can access this
    from the aspx.vb code. However, I want to be able to access this variable
    from a simple class. I've seen where I can have the class inherit from one
    of the web classes, but I don't want to do that just for a simple class that
    needs one variable. Currently, I'm making a public property in the class and
    setting it during the Application_Start() event. But I don't think this is
    the best approach. Is it possible to use the Application() collection from a
    simple class?

    Thanks,

    Brian


    Brian Bischof Guest

  2. Similar Questions and Discussions

    1. #39044 [NEW]: Changing a static variables in a class changes it across sub/super classes.
      From: matti at nitro dot fi Operating system: * PHP version: 5.1.6 PHP Bug Type: Scripting Engine problem Bug description: ...
    2. Classes and declaring variables
      Greetings, all! I have a question about classes and variables. When you are working with a class, when should you (or are required) to declare...
    3. Global variables - application variables vs include file
      What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include...
    4. Variables in classes
      How can I pass a variable out of a class? I'm sure this is very simple, but I can't find out how. I think you put a charater in front of the...
    5. Problems using application state with custom classes
      I am working with vb.net in a asp.net application. I have created a config file in xml format. The goal is to be able to change the config file...
  3. #2

    Default Re: Using Application() variables in non-web classes

    You should look into:

    System.Web.HttpContext.Current.Application

    That will give you the application object of the current web request when
    the code runs.

    HTH,

    bill

    "Brian Bischof" <brian@nospam.bischofsystems.com> wrote in message
    news:ukudZXZQDHA.2320@TK2MSFTNGP12.phx.gbl...
    > Hi All,
    >
    > I saved a variable to the Application() collection and I can access this
    > from the aspx.vb code. However, I want to be able to access this variable
    > from a simple class. I've seen where I can have the class inherit from one
    > of the web classes, but I don't want to do that just for a simple class
    that
    > needs one variable. Currently, I'm making a public property in the class
    and
    > setting it during the Application_Start() event. But I don't think this is
    > the best approach. Is it possible to use the Application() collection from
    a
    > simple class?
    >
    > Thanks,
    >
    > Brian
    >
    >

    William F. Robertson, Jr. Guest

  4. #3

    Default Using Application() variables in non-web classes

    Absolutely...

    Add an import entry to the class:
    System.Web.HttpContext
    Either at the top of your class, or under project
    properties.

    Then, in your class, you reference the
    Session/Application/Server etc objects via:

    {System.Web.HttpContext.}Current.
    {Application|Session|Server etc}

    >-----Original Message-----
    >Hi All,
    >
    >I saved a variable to the Application() collection and I
    can access this
    >from the aspx.vb code. However, I want to be able to
    access this variable
    >from a simple class. I've seen where I can have the class
    inherit from one
    >of the web classes, but I don't want to do that just for
    a simple class that

    David Waz... Guest

  5. #4

    Default Re: Using Application() variables in non-web classes

    Thanks for the quick response. It works great!

    Brian

    "William F. Robertson, Jr." <wfrobertson@kpmg.com> wrote in message
    news:ObIeBbZQDHA.1712@TK2MSFTNGP12.phx.gbl...
    > You should look into:
    >
    > System.Web.HttpContext.Current.Application
    >
    > That will give you the application object of the current web request when
    > the code runs.
    >
    > HTH,
    >
    > bill
    >
    > "Brian Bischof" <brian@nospam.bischofsystems.com> wrote in message
    > news:ukudZXZQDHA.2320@TK2MSFTNGP12.phx.gbl...
    > > Hi All,
    > >
    > > I saved a variable to the Application() collection and I can access this
    > > from the aspx.vb code. However, I want to be able to access this
    variable
    > > from a simple class. I've seen where I can have the class inherit from
    one
    > > of the web classes, but I don't want to do that just for a simple class
    > that
    > > needs one variable. Currently, I'm making a public property in the class
    > and
    > > setting it during the Application_Start() event. But I don't think this
    is
    > > the best approach. Is it possible to use the Application() collection
    from
    > a
    > > simple class?
    > >
    > > Thanks,
    > >
    > > Brian
    > >
    > >
    >
    >

    Brian Bischof Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139