Global的非静态方法 [英] Global's non static methods
问题描述
Global对象可以使用非静态方法吗?
如果答案是肯定的,那么如何在页面中访问它们?
我在Global.asax.cs中有以下属性,但是当我尝试在
页面中使用它时,我收到错误需要对象引用nonstatic
字段,方法或属性CurrentDisplayName":
谢谢,
Alan
公共字符串CurrentDisplayName
{
get
{
string s;
try
{
s =会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] .ToString();
}
catch(例外e)
{
s ="?" ;;
}
返回s;
}
设定
{
会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] =价值;
}
}
这是有道理的。
你必须实例化一个您可以参考之前的类型为Global的实例
a非静态方法。
全球g = new Global();
回复.Write(g.CurrentDisplayName);
或者你可以将CurrentDisplayName声明为静态,你不需要
实例化一个Global类型的对象来引用它。 br />
公共静态字符串CurrentDisplayName
{
//你的实现。
}
Response.Write(Global.CurrentDisplayName);
HTH,
bill
" A.M" < IH ******* @ sapm123.com>在消息中写道
news:e2 ************** @ TK2MSFTNGP10.phx.gbl ...>
Global对象可以使用非静态方法吗?
如果答案是肯定的,那么如何在页面中访问它们?
我在Global.asax.cs中有以下属性,但是当我尝试在
页面中使用它时,我收到错误非静态
字段,方法或属性CurrentDisplayName需要对象引用:
谢谢,
Alan
公共字符串CurrentDisplayName
{
获得
{
字符串s;
尝试
{
s =会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] .ToString();
}
catch(例外e)
{
s ="?" ;;
}
返回s;
}
设置
会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] =值;
}
}
我认为我们不能创建自己的Globa实例l对象。
我确信ASP.NET框架为我们创建了实例。
基于微软在kb#312607中提到的内容,我应该是能够通过使用Page.ApplicationInstance属性获得对该实例的访问权限,但它不会对我有用。
对我来说不起作用。这意味着我无法访问非静态成员!
并且他们不会出现在IDE inlisense中
Alan
" William F. Robertson,Jr。" < WF ********* @ kpmg.com>在消息中写道
新闻:eq ************** @ TK2MSFTNGP09.phx.gbl ...这是有道理的。
你必须实例化一个Global类型的实例,然后才能
引用非静态方法。
全局g = new Global();
Response.Write(g.CurrentDisplayName);
或者您可以将CurrentDisplayName声明为静态,并且您不需要实例化Global类型的对象来引用它。
公共静态字符串CurrentDisplayName
{
//您的实现。
Response.Write(Global.CurrentDisplayName);
HTH,
账单
A.M < IH ******* @ sapm123.com>在消息中写道
新闻:e2 ************** @ TK2MSFTNGP10.phx.gbl ...
> Global对象可以使用非静态方法吗?
如果答案是肯定的,那么如何在页面中访问它们?
我在Global.asax.cs中有以下属性,但是当我尝试在
页面中使用它,我收到错误
非静态字段,方法或属性CurrentDisplayName需要对象引用:
谢谢,
Alan
公共字符串CurrentDisplayName
{
获得
{
字符串s;
尝试
{
s =会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] .ToString();
}
catch(例外e)
{
s ="?" ;;
}
返回s;
}
设置
会话[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}
嗨Alan,
2004年5月5日星期三12:03:00 -0400,A.M < IH ******* @ sapm123.com>
写道:
我认为我们不能创造我们自己的Global对象实例。
这是正确的。
我确信ASP.NET框架为我们创建了实例。
根据微软在kb#312607中提到的内容,我应该可以通过使用Page.ApplicationInstance属性来访问该实例,但它对我不起作用。这意味着我无法访问非静态成员!
并且他们不会出现在IDE inlisense中
我认为这是一个文档中的错误。它应该读取
每个页面都包含类型为
" HttpContext的强类型Context属性。要获得与您的请求关联的全局类的实例
,您可以:
全球g = Context.ApplicationInstance为全局;
if(g!= null)
{
//聚会g
}
>
希望有所帮助。
-
Scott
http://www.OdeToCode.com
Hi,
Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?
I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic
field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}
That makes sense.
You have to instantiate an instance of type Global before you can reference
a non-static method.
Global g = new Global();
Response.Write( g.CurrentDisplayName );
or you can declare CurrentDisplayName as static and you won''t need to
instantiate an object of type Global to reference it.
public static string CurrentDisplayName
{
//your implementation.
}
Response.Write( Global.CurrentDisplayName );
HTH,
bill
"A.M" <IH*******@sapm123.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...Hi,
Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?
I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic
field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}
I don''t think we can create our own instance of Global object.
I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to have
access to that instance by using Page.ApplicationInstance property, But it
doesn''t work for me. That means I don''t have access to non-static members!
and they don''t appear in IDE inlisense
Alan
"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...That makes sense.
You have to instantiate an instance of type Global before you can reference a non-static method.
Global g = new Global();
Response.Write( g.CurrentDisplayName );
or you can declare CurrentDisplayName as static and you won''t need to
instantiate an object of type Global to reference it.
public static string CurrentDisplayName
{
//your implementation.
}
Response.Write( Global.CurrentDisplayName );
HTH,
bill
"A.M" <IH*******@sapm123.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...Hi,
Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?
I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}
Hi Alan,
On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
wrote:
I don''t think we can create our own instance of Global object.
That''s correct.
I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to have
access to that instance by using Page.ApplicationInstance property, But it
doesn''t work for me. That means I don''t have access to non-static members!
and they don''t appear in IDE inlisense
I think that is a bug in the documentation. It should probably read
"every page includes a strongly-typed Context property of type
"HttpContext". To get to the instance of the Global class associated
with your request, you can do:
Global g = Context.ApplicationInstance as Global;
if(g != null)
{
// party on g
}
Hope that helps.
--
Scott
http://www.OdeToCode.com
这篇关于Global的非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!