全球课(简单的问题!) [英] global class (simple question!)

查看:56
本文介绍了全球课(简单的问题!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我想创建一个全局类。要做到这一点,我做了以下几点:


1-创建一个类名测试。它有一个名为mystring的公共变量。

公共类测试


{


public string mystring =" hello world" ;;


}


2-创建一个global.asax及其相应的global.asax.cs(我用< br $>
VC2005)

3 - 在VC2005生成的全局类中,我介绍了测试类如下:


公共类Global:System .Web.HttpApplication


{


public test myclass;


protected void Application_Start(object发件人,EventArgs e)


{


}


protected void Application_End(object sender, EventArgs e)


{


}


}


现在我想在主页中的事件中使用它。


我这样做了:


公共部分类Site1 :System.Web.UI.MasterPage

{


protected void Page_Load(object发件人,EventArgs e)


{


Response.Write(Application.myclass.mystring);


}


}


但我收到此错误:


错误1 ''System.Web.HttpApplicationState''不包含定义

''myclass''


我这样做:


public partial class Site1:System.Web.UI.MasterPage

{


protected void Page_Load(object sender, EventArgs e)


{


Response.Write(Global.myclass.mystring);


}


}


错误1非静态字段,方法或

需要对象引用物业''Global.myclass''


我的代码有什么问题?


问候


Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it using
VC2005)
3 - in global class generated by VC2005, I introduced test class as follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 ''System.Web.HttpApplicationState'' does not contain a definition for
''myclass''

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method, or
property ''Global.myclass''

What is wrong wioth my code?

Regards


推荐答案

你需要将字符串声明为


public static string mys tring =" Hello World";


我建议把它变成一个属性


public static mystring {

get {return" Hello World" ;; }

}


john

www.nicecleanexamples.com

" ma" < ma@nowhere.com写信息

新闻:uz ************** @ TK2MSFTNGP06.phx.gbl ...
You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...

您好,

我想创建一个全局类。要做到这一点,我做了以下几点:


1-创建一个类名测试。它有一个名为mystring的公共变量。

公共类测试


{


public string mystring =" hello world" ;;


}


2-创建一个global.asax及其相应的global.asax.cs(我做了它/>
使用VC2005)

3 - 在VC2005生成的全局类中,我介绍了测试类为

如下:


公共类Global:System.Web.HttpApplication


{


public test myclass;


protected void Application_Start(object sender,EventArgs e)


{


}


protected void Application_End(object sender,EventArgs e)


{


}


}


现在我想在主页的活动中使用它。


我这样做了:


public partial class Site1:System.Web.UI.MasterPage


{


protected void Page_Load(object sende r,EventArgs e)


{


Response.Write(Application.myclass.mystring);


}


}


但我收到此错误:


错误1 ''System.Web.HttpApplicationState''不包含定义

''myclass''


我这样做:


public partial class Site1:System.Web.UI.MasterPage

{


protected void Page_Load(object sender, EventArgs e)


{


Response.Write(Global.myclass.mystring);


}


}


错误1非静态字段需要对象引用,方法,

或property''Global.myclass''


我的代码有什么问题?


问候

Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 ''System.Web.HttpApplicationState'' does not contain a definition
for ''myclass''

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property ''Global.myclass''

What is wrong wioth my code?

Regards



谢谢,但我不希望我的变量是静态的。这是一个例子,但在真实世界中我想要一个不是静态的变量。我该怎么办?


问候


" John Mott" < jo ******** @ hotmail.comwrote in message

news:el ************** @ TK2MSFTNGP03.phx.gbl .. 。
Thanks, but I don ''t want that my variable be static. It is an example but
in realworld I want a variable which is not static. What should I do?

Regards

"John Mott" <jo********@hotmail.comwrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...

你需要将字符串声明为

public static string mystring =" Hello World" ;;


我建议把它变成一个属性


public static mystring {

get {return" Hello World" ;; }

}


john

www.nicecleanexamples.com


" ma" < ma@nowhere.com写信息

新闻:uz ************** @ TK2MSFTNGP06.phx.gbl ...
You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...

>您好,
我想创建一个全局类。为此,我做了以下几点:

1-创建一个类名测试。它有一个名为mystring的公共变量。
公共类测试

公共字符串mystring =" hello world" ;;

创建一个global.asax及其相应的global.asax.cs(我是用VC2005做的)
3 - 在VC2005生成的全局类中,我介绍了测试类如下:

公共类Global:System.Web.HttpApplication

公共测试myclass;
>
protected void Application_Start(object sender,EventArgs e)

{

}

protected void Application_End(object sender,EventArgs e )




现在我想在mater页面的一个事件中使用它。

我这样做了:

公共部分类Site1:System.Web.UI.MasterPage



受保护void Page_Load(object sender,EventArgs e)



Response.Write(Application.myclass.mystring);

}

}

但我开心了g这个错误:

错误1''System.Web.HttpApplicationState''不包含''myclass'的定义

我这样做:

public partial class Site1:System.Web.UI.MasterPage



protected void Page_Load(object sender,EventArgs e)

Response.Write(Global.myclass.mystring);

}

}
错误1非静态字段,方法,
或属性''Global.myclass''需要对象引用

我的代码有什么问题?
问候

>Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 ''System.Web.HttpApplicationState'' does not contain a definition
for ''myclass''

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property ''Global.myclass''

What is wrong wioth my code?

Regards




如果您正在尝试创建全局变量,那么您可以选择希望它们是静态的。

你可以拥有可修改的东西,而不仅仅是一个常数。


static string _mystring ="" ;;


public static string mystring {

get {return _mystring; }

set {_mystring = value;}

}


另一种选择是创建全局类的实例


公共类全球{

公共字符串mystring ="默认值" ;;

};


public Global GlobalInstance = new Global();


然后你可以说


GlobalInstance.mystring ="一个新值;

John

www.nicecleanexamples.com


" ma" < ma@nowhere.com写信息

新闻:OF ************** @ TK2MSFTNGP04.phx.gbl ...
If you''re trying to create global variables you sorta do want them static.
You can have something thats modifiable, not just a constant.

static string _mystring = "";

public static string mystring {
get { return _mystring; }
set { _mystring = value;}
}

The other option is to create an instance of the global class

public class Global {
public string mystring = "a default value";
};

public Global GlobalInstance = new Global();

and then you can say

GlobalInstance.mystring = "a new value";
John
nice clean examples at www.nicecleanexamples.com


"ma" <ma@nowhere.comwrote in message
news:OF**************@TK2MSFTNGP04.phx.gbl...

谢谢,但我不希望我的变量是静态的。这是一个例子,但在真实世界中我想要一个不是静态的变量。我该怎么办?


问候


" John Mott" < jo ******** @ hotmail.comwrote in message

news:el ************** @ TK2MSFTNGP03.phx.gbl .. 。
Thanks, but I don ''t want that my variable be static. It is an example but
in realworld I want a variable which is not static. What should I do?

Regards

"John Mott" <jo********@hotmail.comwrote in message
news:el**************@TK2MSFTNGP03.phx.gbl...

>你需要将字符串声明为

public static string mystring =" Hello World" ;;

我建议把它变成一个属性

public static mystring {
get {return" Hello World" ;; }
}

www .nicecleanexamples.com

" ma" < ma@nowhere.com写信息
新闻:uz ************** @ TK2MSFTNGP06.phx.gbl ...
>You need to declare the string as

public static string mystring = "Hello World";

I would recommend making it a property

public static mystring {
get { return "Hello World"; }
}

john
nice clean examples at www.nicecleanexamples.com
"ma" <ma@nowhere.comwrote in message
news:uz**************@TK2MSFTNGP06.phx.gbl...

>>您好,
我想创建一个全局类。为此,我做了以下几点:

1-创建一个类名测试。它有一个名为mystring的公共变量。
公共类测试

公共字符串mystring =" hello world" ;;

创建一个global.asax及其相应的global.asax.cs(我是用VC2005做的)
3 - 在VC2005生成的全局类中,我介绍了测试类如下:

公共类Global:System.Web.HttpApplication

公共测试myclass;
>
protected void Application_Start(object sender,EventArgs e)

{

}

protected void Application_End(object sender,EventArgs e )




现在我想在mater页面的一个事件中使用它。

我这样做了:

公共部分类Site1:System.Web.UI.MasterPage



受保护void Page_Load(object sender,EventArgs e)



Response.Write(Application.myclass.mystring);

}

}

但我开心了g这个错误:

错误1''System.Web.HttpApplicationState''不包含''myclass'的定义

我这样做:

public partial class Site1:System.Web.UI.MasterPage



protected void Page_Load(object sender,EventArgs e)

Response.Write(Global.myclass.mystring);

}

}
错误1非静态字段,方法,
或属性''Global.myclass''需要对象引用

我的代码有什么问题?
问候

>>Hello,
I want to create a global class. To do this I did the followings:

1- Create a class name test. It has a public variable named mystring.
public class test

{

public string mystring = "hello world";

}

2- Create a global.asax and its coresponding global.asax.cs ( i did it
using VC2005)
3 - in global class generated by VC2005, I introduced test class as
follow:

public class Global : System.Web.HttpApplication

{

public test myclass;

protected void Application_Start(object sender, EventArgs e)

{

}

protected void Application_End(object sender, EventArgs e)

{

}

}

now I want to use it in an event in a mater page.

I did this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Application.myclass.mystring);

}

}

but I am getting this error:

Error 1 ''System.Web.HttpApplicationState'' does not contain a definition
for ''myclass''

I do this:

public partial class Site1 : System.Web.UI.MasterPage

{

protected void Page_Load(object sender, EventArgs e)

{

Response.Write(Global.myclass.mystring);

}

}

Error 1 An object reference is required for the nonstatic field, method,
or property ''Global.myclass''

What is wrong wioth my code?

Regards





这篇关于全球课(简单的问题!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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