469,927 Members | 1,607 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

where to define variable for whoel application?

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
Feb 19 '07 #1
13 1547
On Feb 19, 11:23 pm, "michel" <mm@mmmwrote:
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()

Feb 19 '07 #2
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@mmmwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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


Feb 19 '07 #3
On Feb 20, 12:02 am, "Mark Fitzpatrick" <markf...@fitzme.comwrote:
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

Feb 19 '07 #4
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" <al************@gmail.comschreef in bericht
news:11**********************@s48g2000cws.googlegr oups.com...
On Feb 19, 11:23 pm, "michel" <mm@mmmwrote:
>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()

Feb 19 '07 #5
On Feb 20, 12:24 am, "michel" <mm@mmmwrote:
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" <alexey.smir...@gmail.comschreef in berichtnews:11**********************@s48g2000cws.g ooglegroups.com...
On Feb 19, 11:23 pm, "michel" <mm@mmmwrote:
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

Feb 20 '07 #6
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" <ma******@fitzme.comschreef in bericht
news:eY**************@TK2MSFTNGP02.phx.gbl...
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@mmmwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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



Feb 20 '07 #7
On Feb 20, 12:50 am, "michel" <mm@mmmwrote:
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

Feb 20 '07 #8
On Feb 20, 9:40 am, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
On Feb 20, 12:50 am, "michel" <mm@mmmwrote:
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

Feb 20 '07 #9
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" <al************@gmail.comschreef in bericht
news:11*********************@l53g2000cwa.googlegro ups.com...
On Feb 20, 12:24 am, "michel" <mm@mmmwrote:
>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" <alexey.smir...@gmail.comschreef in
berichtnews:11**********************@s48g2000cws. googlegroups.com...
On Feb 19, 11:23 pm, "michel" <mm@mmmwrote:
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

Feb 20 '07 #10
On Feb 20, 10:14 am, "michel" <mm@mmmwrote:
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)

Feb 20 '07 #11
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" <al************@gmail.comschreef in bericht
news:11**********************@a75g2000cwd.googlegr oups.com...
On Feb 20, 9:40 am, "Alexey Smirnov" <alexey.smir...@gmail.comwrote:
>On Feb 20, 12:50 am, "michel" <mm@mmmwrote:
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

Feb 20 '07 #12
On Feb 20, 10:52 am, "michel" <mm@mmmwrote:
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

Feb 20 '07 #13
Thanks again

"Alexey Smirnov" <al************@gmail.comschreef in bericht
news:11*********************@h3g2000cwc.googlegrou ps.com...
On Feb 20, 10:52 am, "michel" <mm@mmmwrote:
>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

Feb 20 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Thomas Zhu | last post: by
2 posts views Thread by Wilfried Mestdagh | last post: by
2 posts views Thread by JIM.H. | last post: by
18 posts views Thread by Jack | last post: by
9 posts views Thread by blangela | last post: by
5 posts views Thread by MoslyChang | last post: by
2 posts views Thread by Tark Siala | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.