where to define variable for whoel application?

M

michel

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
 
G

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


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()
 
M

Mark Fitzpatrick

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

Guest

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
 
M

michel

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
 
M

michel

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

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

"Anon User" <[email protected]> schreef in bericht





- Show quoted text -


what's this?

Dim odescr As New test
 
G

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.

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
 
G

Guest

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
 
M

michel

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"
 
M

michel

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)
 
G

Guest

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)
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top