在哪里为whoel应用程序定义变量? [英] where to define variable for whoel application?

查看:102
本文介绍了在哪里为whoel应用程序定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i使用方法''descrlimit()''创建了一个类''test''(无论如何)。

那个方法在应用程序的很多页面中使用,所以我需要在这段代码中花费很多时间:Dim odescr As New test


为了避免这种情况,我想知道是否可以将这个

行放在一个中心位置,比如global.asax。


我试过了这个:


班级文件

--------

公共班级考试

公共函数descrlimit(ByVal descr As Object)As String

Dim tmp As String = descr.ToString()

....

返回tmp

结束功能


global.asax

-----------

子的Application_Start(BYVAL发件人为对象,BYVALË作为EventArgs的)

尺寸odescr作为新的测试

Application.Add(QUOT; odescr" ;, odescr)

End Sub


但它会在所有页面中生成使用该方法的错误:名称

''odescr''未申报

感谢您的帮助

Michel

解决方案

2月19日晚上11点23分,michel < mm @ mmmwrote:





i使用方法''descrlimit创建了一个类''test'' ()''(无论如何)。

该方法在应用程序的很多页面中使用,所以我需要花费很多时间来代替这个代码: Dim odescr As New test


为了避免这种情况,我想知道是否可以将

线放在中央位置,喜欢global.asax。


我试过这个:


类文件

------- -

公共类测试

公共函数descrlimit(ByVal descr As Object)As String

Dim tmp As String = descr.ToString()

....

返回tmp

结束功能


global.asax

-----------

Sub Application_Start(ByVal sender As Object,ByVal e As EventArgs)

Dim odescr As New test

Applicati on.Add(" odescr",odescr)

End Sub


但是它会在使用该方法的所有页面中生成错误:Name

''odescr''未申报

感谢您的帮助

Michel



You''ve加入您的变量Application对象,所以你应该

指向它:


尺寸CTX为的HttpApplication = HttpContext.Current。 ApplicationInstance

尺寸TMP作为字符串= ctx.Application(QUOT; odescr")。的ToString()


I''m不确定这会做你的想法。以你b
演示的方式使用它会使应用程序中的所有页面使用完全相同的

对象。这可能不太好,因为你会遇到两个

页面同时对同一项目执行操作的问题。


其中一个你在每个需要的页面重写它所获得的东西

它是在

页面的上下文中实例化和销毁的对象。这有助于内存管理,因此你不需要在不需要时保持或创建相同的对象。此函数是否使用实例化类的任何其他

部分,换句话说,它是否需要访问类中其他地方定义的

方法或属性。例如,一个

矩形对象的CalculateArea方法需要访问宽度和

长度属性。如果你所做的只是将一个值传递给函数

来执行操作并获得值,那么你可能希望

创建一个共享功能。通过这种方式,对象没有被实例化,并且所有页面都可以基本上共享对象的副本,因为他们所做的就是按顺序传递数据。把它拿出去。

-


希望这会有所帮助,

Mark Fitzpatrick

Former Microsoft FrontPage MVP 199?-2006


news:%2 **************** @ TK2MSFTNGP03.phx.gbl ...





i使用方法''descrlimit()'创建了一个类''test''(无论如何)。

该方法在应用程序的很多页面中使用,所以我需要在很长一段时间内输入

这段代码:Dim odescr As New test


为了避免这种情况,我想知道是否可以将这个

行放在一个中心位置,比如global.asax。


我试过这个:


类文件

--------

公共类测试

Public Function descrlimit(ByVal descr As Object)As String

Dim tmp As String = descr.ToString()

....

返回tmp

结束功能


global.asax

-------- ---

Sub Application_Start(ByVal sender As Object,ByVal e As EventArgs)

Dim odescr As New test

Application.Add(" odescr",odescr)

End Sub


但是它会在使用该方法的所有页面中生成错误:名称

''odescr''未声明

感谢您的帮助

Michel



2月20日上午12:02,Mark Fitzpatrick < markf ... @ fitzme.comwrote:


我不确定这会怎样做你的想法。以你的方式使用它
演示将使应用程序中的所有页面使用完全相同的



马克是对的,我忘了这个提到


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

解决方案

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


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



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


这篇关于在哪里为whoel应用程序定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆