where to define variable for whoel application?

Discussion in 'ASP .Net' started by michel, Feb 19, 2007.

  1. michel

    michel Guest

    Hi,

    i created a class 'test' with a method 'descrlimit()' (no matter).
    That method is used in a lot of pages in the application, so i need to put
    this code a lot of time: "Dim odescr As New test"

    In order to avoid that, i wonder whether it would be possible to put that
    line in a central place, like global.asax.

    I tried this:

    class file
    --------
    Public Class test
    Public Function descrlimit(ByVal descr As Object) As String
    Dim tmp As String = descr.ToString()
    ....
    Return tmp
    End Function

    global.asax
    -----------
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim odescr As New test
    Application.Add("odescr", odescr)
    End Sub

    But it generates in all pages which use that method the error: "Name
    'odescr' is not declared"
    Thanks for helping
    Michel
     
    michel, Feb 19, 2007
    #1
    1. Advertising

  2. On Feb 19, 11:23 pm, "michel" <mm@mmm> wrote:
    > Hi,
    >
    > i created a class 'test' with a method 'descrlimit()' (no matter).
    > That method is used in a lot of pages in the application, so i need to put
    > this code a lot of time: "Dim odescr As New test"
    >
    > In order to avoid that, i wonder whether it would be possible to put that
    > line in a central place, like global.asax.
    >
    > I tried this:
    >
    > class file
    > --------
    > Public Class test
    > Public Function descrlimit(ByVal descr As Object) As String
    > Dim tmp As String = descr.ToString()
    > ....
    > Return tmp
    > End Function
    >
    > global.asax
    > -----------
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Dim odescr As New test
    > Application.Add("odescr", odescr)
    > End Sub
    >
    > But it generates in all pages which use that method the error: "Name
    > 'odescr' is not declared"
    > Thanks for helping
    > Michel



    You've added your variable to the Application object, so you should
    refer to it:

    Dim ctx as HttpApplication = HttpContext.Current.ApplicationInstance
    Dim tmp As String = ctx.Application("odescr").ToString()
     
    Alexey Smirnov, Feb 19, 2007
    #2
    1. Advertising

  3. I'm not sure this is going to do what you think. Using it the way you
    demonstrate would have all the pages in the application using the exact same
    object. That's probably not good since you will run into issues where two
    pages are performing actions on the same item at one time.

    One of the things that you gain from re-writing it in each page that needs
    it is the object is instantiated and destroyed within the context of that
    page. That helps memory management so you don't have that same object being
    held on to or created when it isn't needed. Does this function use any other
    portions of an instantiated class, in other words does it need access to
    methods or properties defined elsewhere in the class. For example, a
    rectangle objects CalculateArea method would need access to the width and
    length properties. If all you're doing is passing a value into the function
    to perform an operation and get the value out, you may want, instead, to
    create a shared function. This way the object isn't instantiated and all
    your pages can essentially share a copy of the object since all they're
    doing is passing their data in order to get it out.


    --

    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "michel" <mm@mmm> wrote in message
    news:%...
    > Hi,
    >
    > i created a class 'test' with a method 'descrlimit()' (no matter).
    > That method is used in a lot of pages in the application, so i need to put
    > this code a lot of time: "Dim odescr As New test"
    >
    > In order to avoid that, i wonder whether it would be possible to put that
    > line in a central place, like global.asax.
    >
    > I tried this:
    >
    > class file
    > --------
    > Public Class test
    > Public Function descrlimit(ByVal descr As Object) As String
    > Dim tmp As String = descr.ToString()
    > ....
    > Return tmp
    > End Function
    >
    > global.asax
    > -----------
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Dim odescr As New test
    > Application.Add("odescr", odescr)
    > End Sub
    >
    > But it generates in all pages which use that method the error: "Name
    > 'odescr' is not declared"
    > Thanks for helping
    > Michel
    >
    >
     
    Mark Fitzpatrick, Feb 19, 2007
    #3
  4. On Feb 20, 12:02 am, "Mark Fitzpatrick" <> wrote:
    > I'm not sure this is going to do what you think. Using it the way you
    > demonstrate would have all the pages in the application using the exact same


    Mark is right, I forgot this to mention
     
    Alexey Smirnov, Feb 19, 2007
    #4
  5. michel

    michel Guest

    Hi Alexey, thanks for replying ...
    I made the changes but still same error: Name 'odescr' is not declared"

    global.asax: (unchanged):
    ----------
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim odescr As New test
    Application.Add("odescr", odescr)
    End Sub

    class fle:
    --------
    Public Class test
    Public Function descrlimit(ByVal descr As Object) As String
    Dim ctx As HttpApplication = HttpContext.Current.ApplicationInstance
    Dim tmp As String = ctx.Application("odescr").ToString()
    ....
    Return tmp
    End Function
    End Class










    "Alexey Smirnov" <> schreef in bericht
    news:...
    > On Feb 19, 11:23 pm, "michel" <mm@mmm> wrote:
    >> Hi,
    >>
    >> i created a class 'test' with a method 'descrlimit()' (no matter).
    >> That method is used in a lot of pages in the application, so i need to
    >> put
    >> this code a lot of time: "Dim odescr As New test"
    >>
    >> In order to avoid that, i wonder whether it would be possible to put that
    >> line in a central place, like global.asax.
    >>
    >> I tried this:
    >>
    >> class file
    >> --------
    >> Public Class test
    >> Public Function descrlimit(ByVal descr As Object) As String
    >> Dim tmp As String = descr.ToString()
    >> ....
    >> Return tmp
    >> End Function
    >>
    >> global.asax
    >> -----------
    >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >> Dim odescr As New test
    >> Application.Add("odescr", odescr)
    >> End Sub
    >>
    >> But it generates in all pages which use that method the error: "Name
    >> 'odescr' is not declared"
    >> Thanks for helping
    >> Michel

    >
    >
    > You've added your variable to the Application object, so you should
    > refer to it:
    >
    > Dim ctx as HttpApplication = HttpContext.Current.ApplicationInstance
    > Dim tmp As String = ctx.Application("odescr").ToString()
    >
     
    michel, Feb 19, 2007
    #5
  6. michel

    michel Guest

    Hi, thanks for your explanation..
    But could you tell me what 's wrong in this code, for the sake of my spirit
    ....? I still get that error.

    "Mark Fitzpatrick" <> schreef in bericht
    news:...
    > I'm not sure this is going to do what you think. Using it the way you
    > demonstrate would have all the pages in the application using the exact
    > same object. That's probably not good since you will run into issues where
    > two pages are performing actions on the same item at one time.
    >
    > One of the things that you gain from re-writing it in each page that needs
    > it is the object is instantiated and destroyed within the context of that
    > page. That helps memory management so you don't have that same object
    > being held on to or created when it isn't needed. Does this function use
    > any other portions of an instantiated class, in other words does it need
    > access to methods or properties defined elsewhere in the class. For
    > example, a rectangle objects CalculateArea method would need access to the
    > width and length properties. If all you're doing is passing a value into
    > the function to perform an operation and get the value out, you may want,
    > instead, to create a shared function. This way the object isn't
    > instantiated and all your pages can essentially share a copy of the object
    > since all they're doing is passing their data in order to get it out.
    >
    >
    > --
    >
    > Hope this helps,
    > Mark Fitzpatrick
    > Former Microsoft FrontPage MVP 199?-2006
    >
    > "michel" <mm@mmm> wrote in message
    > news:%...
    >> Hi,
    >>
    >> i created a class 'test' with a method 'descrlimit()' (no matter).
    >> That method is used in a lot of pages in the application, so i need to
    >> put this code a lot of time: "Dim odescr As New test"
    >>
    >> In order to avoid that, i wonder whether it would be possible to put that
    >> line in a central place, like global.asax.
    >>
    >> I tried this:
    >>
    >> class file
    >> --------
    >> Public Class test
    >> Public Function descrlimit(ByVal descr As Object) As String
    >> Dim tmp As String = descr.ToString()
    >> ....
    >> Return tmp
    >> End Function
    >>
    >> global.asax
    >> -----------
    >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >> Dim odescr As New test
    >> Application.Add("odescr", odescr)
    >> End Sub
    >>
    >> But it generates in all pages which use that method the error: "Name
    >> 'odescr' is not declared"
    >> Thanks for helping
    >> Michel
    >>
    >>

    >
    >
     
    michel, Feb 19, 2007
    #6
  7. On Feb 20, 12:24 am, "michel" <mm@mmm> wrote:
    > Hi Alexey, thanks for replying ...
    > I made the changes but still same error: Name 'odescr' is not declared"
    >
    > global.asax: (unchanged):
    > ----------
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Dim odescr As New test
    > Application.Add("odescr", odescr)
    > End Sub
    >
    > class fle:
    > --------
    > Public Class test
    > Public Function descrlimit(ByVal descr As Object) As String
    > Dim ctx As HttpApplication = HttpContext.Current.ApplicationInstance
    > Dim tmp As String = ctx.Application("odescr").ToString()
    > ....
    > Return tmp
    > End Function
    > End Class
    >
    > "Alexey Smirnov" <> schreef in berichtnews:...
    >
    >
    >
    > > On Feb 19, 11:23 pm, "michel" <mm@mmm> wrote:
    > >> Hi,

    >
    > >> i created a class 'test' with a method 'descrlimit()' (no matter).
    > >> That method is used in a lot of pages in the application, so i need to
    > >> put
    > >> this code a lot of time: "Dim odescr As New test"

    >
    > >> In order to avoid that, i wonder whether it would be possible to put that
    > >> line in a central place, like global.asax.

    >
    > >> I tried this:

    >
    > >> class file
    > >> --------
    > >> Public Class test
    > >> Public Function descrlimit(ByVal descr As Object) As String
    > >> Dim tmp As String = descr.ToString()
    > >> ....
    > >> Return tmp
    > >> End Function

    >
    > >> global.asax
    > >> -----------
    > >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > >> Dim odescr As New test
    > >> Application.Add("odescr", odescr)
    > >> End Sub

    >
    > >> But it generates in all pages which use that method the error: "Name
    > >> 'odescr' is not declared"
    > >> Thanks for helping
    > >> Michel

    >
    > > You've added your variable to the Application object, so you should
    > > refer to it:

    >
    > > Dim ctx as HttpApplication = HttpContext.Current.ApplicationInstance
    > > Dim tmp As String = ctx.Application("odescr").ToString()- Hide quoted text -

    >
    > - Show quoted text -



    what's this?

    Dim odescr As New test
     
    Alexey Smirnov, Feb 19, 2007
    #7
  8. On Feb 20, 12:50 am, "michel" <mm@mmm> wrote:
    > Hi, thanks for your explanation..
    > But could you tell me what 's wrong in this code, for the sake of my spirit
    > ...? I still get that error.


    The code must be as

    class file
    --------
    Public Class test
    Public Function descrlimit(ByVal descr As Object) As String
    Dim tmp As String = descr.ToString()
    ....
    Return tmp
    End Function


    global.asax
    -----------
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Application.Add("odescr", "here is my value for odescr")
    End Sub


    You got the error because odescr was not initialized
     
    Alexey Smirnov, Feb 20, 2007
    #8
  9. On Feb 20, 9:40 am, "Alexey Smirnov" <> wrote:
    > On Feb 20, 12:50 am, "michel" <mm@mmm> wrote:
    >
    > > Hi, thanks for your explanation..
    > > But could you tell me what 's wrong in this code, for the sake of my spirit
    > > ...? I still get that error.

    >
    > The code must be as
    >
    > class file
    > --------
    > Public Class test
    > Public Function descrlimit(ByVal descr As Object) As String
    > Dim tmp As String = descr.ToString()
    > ....
    > Return tmp
    > End Function
    >
    > global.asax
    > -----------
    > Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    > Application.Add("odescr", "here is my value for odescr")
    > End Sub
    >
    > You got the error because odescr was not initialized


    sorry, the class file needs to be modified

    class file
    --------
    Public Class test
    Public Function descrlimit() As String

    Dim ctx as HttpApplication =
    HttpContext.Current.ApplicationInstance
    Dim tmp As String = ctx.Application("odescr").ToString()

    Return tmp
    End Function
     
    Alexey Smirnov, Feb 20, 2007
    #9
  10. michel

    michel Guest

    That's the code that i used to put in each aspx page for creation an
    instance of class 'test' and that i want to move to the global.asax.

    e.g. (before moving it in global.asax)

    Partial Class final
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim odescr As New descr
    anyvariable = odescr.descrlimit(anyvariable)
    .........


    It works, but if i remove "Dim odescr As New descr" from here and put it
    into glbal.asax, i get the error: 'odescr' is not declared"







    "Alexey Smirnov" <> schreef in bericht
    news:...
    > On Feb 20, 12:24 am, "michel" <mm@mmm> wrote:
    >> Hi Alexey, thanks for replying ...
    >> I made the changes but still same error: Name 'odescr' is not declared"
    >>
    >> global.asax: (unchanged):
    >> ----------
    >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >> Dim odescr As New test
    >> Application.Add("odescr", odescr)
    >> End Sub
    >>
    >> class fle:
    >> --------
    >> Public Class test
    >> Public Function descrlimit(ByVal descr As Object) As String
    >> Dim ctx As HttpApplication =
    >> HttpContext.Current.ApplicationInstance
    >> Dim tmp As String = ctx.Application("odescr").ToString()
    >> ....
    >> Return tmp
    >> End Function
    >> End Class
    >>
    >> "Alexey Smirnov" <> schreef in
    >> berichtnews:...
    >>
    >>
    >>
    >> > On Feb 19, 11:23 pm, "michel" <mm@mmm> wrote:
    >> >> Hi,

    >>
    >> >> i created a class 'test' with a method 'descrlimit()' (no matter).
    >> >> That method is used in a lot of pages in the application, so i need to
    >> >> put
    >> >> this code a lot of time: "Dim odescr As New test"

    >>
    >> >> In order to avoid that, i wonder whether it would be possible to put
    >> >> that
    >> >> line in a central place, like global.asax.

    >>
    >> >> I tried this:

    >>
    >> >> class file
    >> >> --------
    >> >> Public Class test
    >> >> Public Function descrlimit(ByVal descr As Object) As String
    >> >> Dim tmp As String = descr.ToString()
    >> >> ....
    >> >> Return tmp
    >> >> End Function

    >>
    >> >> global.asax
    >> >> -----------
    >> >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >> >> Dim odescr As New test
    >> >> Application.Add("odescr", odescr)
    >> >> End Sub

    >>
    >> >> But it generates in all pages which use that method the error: "Name
    >> >> 'odescr' is not declared"
    >> >> Thanks for helping
    >> >> Michel

    >>
    >> > You've added your variable to the Application object, so you should
    >> > refer to it:

    >>
    >> > Dim ctx as HttpApplication = HttpContext.Current.ApplicationInstance
    >> > Dim tmp As String = ctx.Application("odescr").ToString()- Hide quoted
    >> > text -

    >>
    >> - Show quoted text -

    >
    >
    > what's this?
    >
    > Dim odescr As New test
    >
     
    michel, Feb 20, 2007
    #10
  11. michel

    michel Guest

    I think i'm still doing somehing wrong: because i have still the same error:

    global.asax:
    -----------
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Application.Add("odescr", New test())
    End Sub

    class test:
    ---------
    Public Class descr
    Public Function descrlimit() As String
    Dim ctx As HttpApplication = HttpContext.Current.ApplicationInstance
    Dim tmp As String = ctx.Application("odescr").ToString()
    .....
    Return tmp
    End Function
    End Class


    in any aspx file using class test'
    -------------------------------
    myvariable= odescr.descrlimit(myvariable)






    "Alexey Smirnov" <> schreef in bericht
    news:...
    > On Feb 20, 9:40 am, "Alexey Smirnov" <> wrote:
    >> On Feb 20, 12:50 am, "michel" <mm@mmm> wrote:
    >>
    >> > Hi, thanks for your explanation..
    >> > But could you tell me what 's wrong in this code, for the sake of my
    >> > spirit
    >> > ...? I still get that error.

    >>
    >> The code must be as
    >>
    >> class file
    >> --------
    >> Public Class test
    >> Public Function descrlimit(ByVal descr As Object) As String
    >> Dim tmp As String = descr.ToString()
    >> ....
    >> Return tmp
    >> End Function
    >>
    >> global.asax
    >> -----------
    >> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    >> Application.Add("odescr", "here is my value for odescr")
    >> End Sub
    >>
    >> You got the error because odescr was not initialized

    >
    > sorry, the class file needs to be modified
    >
    > class file
    > --------
    > Public Class test
    > Public Function descrlimit() As String
    >
    > Dim ctx as HttpApplication =
    > HttpContext.Current.ApplicationInstance
    > Dim tmp As String = ctx.Application("odescr").ToString()
    >
    > Return tmp
    > End Function
    >
     
    michel, Feb 20, 2007
    #11
  12. On Feb 20, 10:14 am, "michel" <mm@mmm> wrote:

    > It works, but if i remove "Dim odescr As New descr" from here and put it
    > into glbal.asax, i get the error: 'odescr' is not declared"
    >


    Of course, "Dim odescr As New descr" is a declaration for odescr
    variable.

    If you want to refer to the value of the key you added into
    Application object, you should use this Application object.

    So, when you do this

    Application.Add("odescr", "here is my value for odescr")

    You add a key named "odescr" and its value into Application object.
    This value will be shared accross the application and not for a
    specific user.

    To get the value you should use something like this

    Dim ctx as HttpApplication =
    HttpContext.Current.ApplicationInstance
    Dim anyvariable As String =
    ctx.Application("odescr").ToString() ' if odescr is a string

    and forget about

    anyvariable = odescr.descrlimit(anyvariable)
     
    Alexey Smirnov, Feb 20, 2007
    #12
  13. Alexey Smirnov, Feb 20, 2007
    #13
  14. michel

    michel Guest

    Thanks again

    "Alexey Smirnov" <> schreef in bericht
    news:...
    > On Feb 20, 10:52 am, "michel" <mm@mmm> wrote:
    >> I think i'm still doing somehing wrong: because i have still the same
    >> error:

    >
    > Instead this line
    >
    > myvariable= odescr.descrlimit(myvariable)
    >
    > you have to call Application object.
    >
    > myvariable= Application("odescr")
    >
    > Please check this article
    >
    > Using Session and Application Objects in ASP .NET
    > http://msdn.microsoft.com/msdnmag/issues/01/11/cutting/
    > http://msdn.microsoft.com/msdnmag/issues/01/12/cutting/
    >
    > Hope it helps
    >
     
    michel, Feb 20, 2007
    #14
    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. theotyflos
    Replies:
    3
    Views:
    482
    Thomas Matthews
    Feb 19, 2004
  2. robin liu
    Replies:
    3
    Views:
    832
    Robin Liu
    Apr 21, 2006
  3. mfglinux
    Replies:
    11
    Views:
    725
    Roberto Bonvallet
    Sep 12, 2007
  4. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    481
    Brian Takita
    Jan 23, 2006
  5. David Filmer
    Replies:
    19
    Views:
    257
    Kevin Collins
    May 21, 2004
Loading...

Share This Page