473,320 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1688
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt Clepper | last post by:
Any way to do this? I need to call functions based on a variable. Do I actually have to make a case statement and call each funciton explicitly, or is there any way to call a function where the...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
13
by: Thomas Zhu | last post by:
Hello, I know the difference between the two definations. But I do not know where are they in the memory. would someone tell me ? char s={"good", "morning"}; // at stack? char *t = {"good",...
2
by: Wilfried Mestdagh | last post by:
Hi, If I put a #define somewhere in a file then the compiler complains with 'cannot define/undefine after first token in file' So I plase #define in very begin of file, but then compiler say...
2
by: JIM.H. | last post by:
Hello, In my C# asp.net web application, I need to define a few variable that is reachable from all pages. If it was C I would define it in the .h file as struct and use it in the...
18
by: Jack | last post by:
Thanks.
9
by: blangela | last post by:
Can somepoint me to a good discussion on the pros and cons of using #define versus a constant variable in your C+ application? Thanks, Bob
5
by: MoslyChang | last post by:
Hi, All When I look at effective c++,item2 and item3. I have some basic questions , Does anyone be familar with this topic? it suggests const is perfer to #define, then I think how to replace...
2
by: Tark Siala | last post by:
hi i worked with VB6 then i changed to C#. i want define Variable thats i can accessed from any Form or Class in my project. in VB6 you can do that by define as "Global" in Module, but in C# i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.