NonStatic erorr [英] NonStatic erorr

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

问题描述

我对C#有点新,很难理解为什么我会收到

非静态错误。我通过使用控件在我的主窗体中创建一个具有成员

属性的对象。从另一个表单/对话框中,我尝试访问

对象来设置属性。我收到一个非静态错误。我可以改变我的对象

到静态,这很好。静态对象按我的预期运行。非

静态对象不是。我不明白为什么会这样。我已经公开了,并且我的项目名称空间相同。似乎我只能在我创建它的同一个类中使用一个对象。在这种情况下

我在我的主窗体类中创建了我的对象并且可以访问很容易从该类中的任何

控件中设置任何属性,但是如果我尝试从另一个表单/对话框类访问这个

对象,我会收到一个非静态错误。


Thx,

解决方案

嗨松饼,

你有一个简单的你要做什么的代码示例?


马克。

-
http://www.markdawson.org

" Muffin"写道:


我对C#有点新,并且很难理解为什么我会得到一个

非静态错误。我通过使用控件在我的主窗体中创建一个具有成员

属性的对象。从另一个表单/对话框中,我尝试访问

对象来设置属性。我收到一个非静态错误。我可以改变我的对象

到静态,这很好。静态对象按我的预期运行。非

静态对象不是。我不明白为什么会这样。我已经公开了,并且我的项目名称空间相同。似乎我只能在我创建它的同一个类中使用一个对象。在这种情况下

我在我的主窗体类中创建了我的对象并且可以访问很容易从该类中的任何

控件中设置任何属性,但是如果我尝试从另一个表单/对话框类访问这个

对象,我会收到一个非静态错误。


Thx,


Muffin< mu **** @ NoEmail.localwrote:
< blockquote class =post_quotes>
我对C#有点新,并且很难理解为什么我得到一个

非静态错误。我通过使用控件在我的主窗体中创建一个具有成员

属性的对象。从另一个表单/对话框中,我尝试访问

对象来设置属性。我收到一个非静态错误。我可以改变我的对象

到静态,这很好。静态对象按我的预期运行。非

静态对象不是。我不明白为什么会这样。我已经公开了,并且我的项目名称空间相同。似乎我只能在我创建它的同一个类中使用一个对象。在这种情况下

我在我的主窗体类中创建了我的对象并且可以访问很容易从该类中的任何

控件中设置任何属性,但是如果我尝试从另一个表单/对话框类访问这个

对象,我会收到一个非静态错误。



你能发一个简短但完整的程序来演示

的问题吗?


http://www.pobox.com/~skeet/csharp/有关

的详细信息,请参阅完整版本
。我的意思是什么。


请记住,没有像b这样的东西;静态对象或非

静态对象 - 但是如果你试图在静态方法中从

访问一个实例*变量*,你需要指定你在讨论哪个实例。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请执行不要给我发邮件


我正在尝试做的事情非常简单。


我创建一个带有成员变量的类


公共课MyObject

{

公共字符串名称;

}


他们从主框架内创建了一个对象的实例。然后我

有一个启动对话框的控件。从该对话框中我尝试

更新对象(EditMe.name)


class Frame1:frame

{

MyObject EditMe = new MyObject();框架启动时创建实例


SomeEventHandle()

{

只需创建对话框类的实例

}

SomeOtherControlHandle()

{

EditMe.name =" ThisWorks" ---我可以毫无问题地做到这一点

}

类对话框:框架

{

SomeControlHandle( )

{

EditMe.name =" test"; --->我无法编辑这个

}

}


我不明白为什么我无法在

对话框或其他类的实例中看到EditMe。


" Jon Skeet [C#MVP]" < sk *** @ pobox.com写了留言

新闻:MP ************************ @ msnews。 microsoft.c om ...


Muffin< mu **** @ NoEmail.localwrote:


>我对C#有点新,并且很难理解为什么我会得到一个
非静态错误。我通过使用控件在我的主窗体中创建一个具有成员
属性的对象。从另一个表单/对话框我尝试访问
对象来设置属性。我收到一个非静态错误。我可以将我的
对象改为静态,这很好用。静态对象按我的预期运行。非
静态对象不是。我不明白为什么会这样。我已将其公开,并且与我的项目名称空间相同。似乎我只能在我创建它的同一个类中使用一个对象。在这个
的情况下
我在我的主窗体类中创建了我的对象并且可以访问很容易从该类中的任何
控件中设置任何属性,但是如果我尝试从另一个表单/对话框类访问这个
对象,我会收到一个非静态错误。



你能发一个简短但完整的程序来演示

的问题吗?


http://www.pobox.com/~skeet/csharp/有关

的详细信息,请参阅完整版本
。我的意思是什么。


请记住,没有像b这样的东西;静态对象或非

静态对象 - 但是如果你试图在静态方法中从

访问一个实例*变量*,你需要指定你在讨论哪个实例。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请执行不要给我发邮件



I am a little new to C# and an have a hard time understanding why I get a
nonstatic error. I create an object in my main form that has member
properties by using a control. From another form/dialog I try to access that
object to set a property. I get a non static error. I can change my object
to static, which works fine. Static objects are acting as I expect. Non
static objects are not. I do not understand why this is happening. I have
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.

Thx,

解决方案

Hi Muffin,
do you have a simple code example of what you are trying to do?

Mark.
--
http://www.markdawson.org
"Muffin" wrote:

I am a little new to C# and an have a hard time understanding why I get a
nonstatic error. I create an object in my main form that has member
properties by using a control. From another form/dialog I try to access that
object to set a property. I get a non static error. I can change my object
to static, which works fine. Static objects are acting as I expect. Non
static objects are not. I do not understand why this is happening. I have
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.

Thx,


Muffin <mu****@NoEmail.localwrote:

I am a little new to C# and an have a hard time understanding why I get a
nonstatic error. I create an object in my main form that has member
properties by using a control. From another form/dialog I try to access that
object to set a property. I get a non static error. I can change my object
to static, which works fine. Static objects are acting as I expect. Non
static objects are not. I do not understand why this is happening. I have
made it public and am in the same name space for my project. It seem that I
can only use an object in the same class that I created it in. In this case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access this
object from another form/dialog class I recieve a non static error.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there''s no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you''ll need to specify which instance you''re
talking about.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


It is really simple what I am try to do.

I create a class with a member variable

public class MyObject
{
public string name;
}

Them from within the main frame I create an instance of the object. Then I
have a control that launches a dialog. From within that dialog I try to
update the object (EditMe.name)

class Frame1: frame
{
MyObject EditMe = new MyObject(); create instance when frame is launched

SomeEventHandle()
{
just create an instance of dialog class
}
SomeOtherControlHandle()
{
EditMe.name="ThisWorks" ---I can do this with no problem
}
class dialog: frame
{
SomeControlHandle()
{
EditMe.name="test";--->I can not edit this
}
}

I do not understand why I can not see EditMe from within an instance of
dialog or another class for that matter.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...

Muffin <mu****@NoEmail.localwrote:

>I am a little new to C# and an have a hard time understanding why I get a
nonstatic error. I create an object in my main form that has member
properties by using a control. From another form/dialog I try to access
that
object to set a property. I get a non static error. I can change my
object
to static, which works fine. Static objects are acting as I expect. Non
static objects are not. I do not understand why this is happening. I
have
made it public and am in the same name space for my project. It seem that
I
can only use an object in the same class that I created it in. In this
case
I created my object in my main form class and can access easly from any
control in that class and set any property, however if I try to access
this
object from another form/dialog class I recieve a non static error.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Bear in mind that there''s no such thing as a "static object" or "non-
static object" - but if you try to access an instance *variable* from
within a static method, you''ll need to specify which instance you''re
talking about.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too



这篇关于NonStatic erorr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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