Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > where to define variable for whoel application?

Reply
Thread Tools

where to define variable for whoel application?

 
 
michel
Guest
Posts: n/a
 
      02-19-2007
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


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-19-2007
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()

 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      02-19-2007
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:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-19-2007
On Feb 20, 12:02 am, "Mark Fitzpatrick" <(E-Mail Removed)> 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

 
Reply With Quote
 
michel
Guest
Posts: n/a
 
      02-19-2007
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) oups.com...
> 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()
>



 
Reply With Quote
 
michel
Guest
Posts: n/a
 
      02-19-2007
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed)...
> 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:%(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-19-2007
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" <(E-Mail Removed)> schreef in berichtnews:(E-Mail Removed) ooglegroups.com...
>
>
>
> > 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

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-20-2007
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

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      02-20-2007
On Feb 20, 9:40 am, "Alexey Smirnov" <(E-Mail Removed)> 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

 
Reply With Quote
 
michel
Guest
Posts: n/a
 
      02-20-2007
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" <(E-Mail Removed)> schreef in bericht
news:(E-Mail Removed) ups.com...
> 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" <(E-Mail Removed)> schreef in
>> berichtnews:(E-Mail Removed) ooglegroups.com...
>>
>>
>>
>> > 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments